Commit ef203c2b
ef203c2b5883Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 12 files · +10 added · ~2 modified
Description
Merge pull request #1271 from fc-anjos/feature/implicit-allocation-site-specialization
Specialize implicit allocation-site helper classes
Details
- Commit
- ef203c2b5883
- Parent
- e6bdc7d8f6dd
- Merge parent
- 41c21316a608
spinel_analyze.rbmodifiedView file
@@ -333,6 +333,14 @@
333333
# with individual scalar locals. Distinct from value_type: SRA allows334334
# attr_writer (mutation is rewritten to per-field assignment).335335
@cls_is_sra = []336+
# Direct `Class.new` allocation sites that were cloned into a337+
# per-site class by collect_implicit_new_specializations. The338+
# source syntax stays ordinary Ruby; inference sees the cloned339+
# class so ivar/method tables can specialize independently.340+
@implicit_new_site_ids = []341+
@implicit_new_site_classes = "".split(",", -1)342+
@implicit_new_local_names = "".split(",", -1)343+
@implicit_new_local_classes = "".split(",", -1)336344
337345
# ---- Constants (parallel arrays) ----338346
@const_names = "".split(",", -1)@@ -884,6 +892,34 @@
884892
""885893
end886894
895+
def implicit_new_site_class(nid)896+
k = 0897+
while k < @implicit_new_site_ids.length898+
if @implicit_new_site_ids[k] == nid899+
return @implicit_new_site_classes[k]900+
end901+
k = k + 1902+
end903+
""904+
end905+
906+
def implicit_new_local_class(name)907+
result = ""908+
k = 0909+
while k < @implicit_new_local_names.length910+
if @implicit_new_local_names[k] == name911+
cls = @implicit_new_local_classes[k]912+
if result == ""913+
result = cls914+
elsif result != cls915+
return ""916+
end917+
end918+
k = k + 1919+
end920+
result921+
end922+
887923
def module_name_exists(name)888924
i = 0889925
while i < @module_names.length@@ -2329,6 +2365,10 @@
23292365
if vt != ""23302366
return vt23312367
end2368+
implicit_cls = implicit_new_local_class(@nd_name[nid])2369+
if implicit_cls != ""2370+
return "obj_" + implicit_cls2371+
end23322372
return "int"23332373
end23342374
if t == "LocalVariableWriteNode"@@ -6703,6 +6743,10 @@
67036743
67046744
def infer_constructor_type(nid, mname, recv)67056745
if mname == "new"6746+
spec_cname = implicit_new_site_class(nid)6747+
if spec_cname != ""6748+
return "obj_" + spec_cname6749+
end67066750
# Implicit recv-less `new` in a class method body resolves67076751
# to `obj_<CurrentClass>` so subsequent attr_writer calls67086752
# and ivar widening see the right type.@@ -8516,6 +8560,12 @@
85168560
# (last-include-wins); user-defined top-level `def`s are preserved.85178561
# Must run after module-function entries are registered.85188562
collect_toplevel_module_includes8563+
8564+
# Pass 2.55: clone direct allocation sites for classes with8565+
# empty-array ivar initialization before receiver-method prepasses8566+
# can merge sibling instances into one class table.8567+
collect_implicit_new_specializations8568+
85198569
# Pass 2.6: hoist `recv.instance_eval do ... end` blocks into85208570
# file-scope static functions. Receiver-class flow analysis picks the85218571
# receiver's class, the block body is later compiled as a function@@ -9218,158 +9268,648 @@
92189268
end92199269
end92209270
9221-
def collect_scoped_constant(scope_name, nid)9222-
cname = @nd_name[nid]9223-
if scope_name != ""9224-
cname = scope_name + "_" + cname9271+
def node_contains_empty_array_ivar_write(nid)9272+
if nid < 09273+
return 09274+
end9275+
t = @nd_type[nid]9276+
if t == "DefNode" || t == "ClassNode" || t == "ModuleNode" || t == "SingletonClassNode"9277+
return 092259278
end9226-
expr_id = @nd_expression[nid]9227-
if expr_id >= 09228-
# `Foo = Struct.new(:a, :b)` and `Foo = Data.define(:a, :b)` share9229-
# a structural contract: positional symbol args naming the fields.9230-
# Data is keyword-init at runtime (`Foo.new(a: 1, b: 2)`), which9231-
# the existing kwarg constructor path already matches by name.9232-
# Routing both through `collect_struct_class` registers the9233-
# synthetic class so codegen has a `sp_<Foo>` type to emit;9234-
# without this the AST-emitted `obj_<Foo>` reference resolves to9235-
# an undeclared struct, tripping clang `unknown type name9236-
# 'sp_<Foo>'`.9237-
if is_call_on_const(expr_id, "Struct", "new") == 1 || is_call_on_const(expr_id, "Data", "define") == 19238-
collect_struct_class(cname, expr_id)9239-
return9279+
if t == "InstanceVariableWriteNode"9280+
if is_empty_array_literal(@nd_expression[nid]) == 19281+
return 192409282
end92419283
end9242-
ct = "int"9243-
if expr_id >= 09244-
old_scope = @current_lexical_scope9245-
@current_lexical_scope = scope_name9246-
ct = infer_type(expr_id)9247-
@current_lexical_scope = old_scope9284+
cs = []9285+
push_child_ids(nid, cs)9286+
k = 09287+
while k < cs.length9288+
if node_contains_empty_array_ivar_write(cs[k]) == 19289+
return 19290+
end9291+
k = k + 192489292
end9249-
ci = find_const_idx(cname)9250-
if ci >= 09251-
@const_types[ci] = ct9252-
@const_expr_ids[ci] = expr_id9253-
@const_scope_names[ci] = scope_name9254-
return9255-
end9256-
@const_names.push(cname)9257-
@const_types.push(ct)9258-
@const_expr_ids.push(expr_id)9259-
@const_scope_names.push(scope_name)9293+
092609294
end92619295
9262-
# Register a single method as an open-class top-level function9263-
# (`__oc_<cname>_<mname>`) -- the form primitive dispatch resolves for9264-
# built-in reopens (`class Integer; def to_words`). Shared by the9265-
# direct-def and include-into-built-in paths.9266-
def register_open_class_def(cname, mname, sid)9267-
@meth_names.push("__oc_" + cname + "_" + mname)9268-
@meth_param_names.push(collect_params_str(sid))9269-
@meth_param_types.push(collect_ptypes_str(sid, -1))9270-
@meth_param_empty.push("")9271-
@meth_return_types.push("int")9272-
@meth_body_ids.push(@nd_body[sid])9273-
@meth_has_yield.push(0)9274-
@meth_has_defaults.push("0")9275-
@meth_rest_index.push(collect_rest_index(sid))9276-
@meth_kwrest_index.push(collect_kwrest_index(sid))9296+
def implicit_specializable_class?(ci)9297+
if ci < 0 || ci >= @cls_names.length9298+
return 09299+
end9300+
if @cls_is_value_type[ci] == 19301+
return 09302+
end9303+
init_idx = cls_find_method_direct(ci, "initialize")9304+
if init_idx < 09305+
return 09306+
end9307+
bodies = @cls_meth_bodies[ci].split(";", -1)9308+
if init_idx >= bodies.length9309+
return 09310+
end9311+
bid = bodies[init_idx].to_i9312+
if bid < 09313+
return 09314+
end9315+
node_contains_empty_array_ivar_write(bid)92779316
end92789317
9279-
# Register the instance methods of an included module as open-class9280-
# functions on a reopened built-in (`class Integer; include M`). Skips9281-
# `def self.x` module functions and names already registered (so a9282-
# direct def or a second include doesn't duplicate the C function).9283-
def register_open_class_module_methods(cname, inc_nid, module_prefix)9284-
inc_t = @nd_type[inc_nid]9285-
if inc_t != "ConstantReadNode" && inc_t != "ConstantPathNode"9318+
def collect_implicit_new_site_candidates(nid, site_ids, site_classes)9319+
if nid < 092869320
return92879321
end9288-
mod_name = const_ref_flat_name(inc_nid)9289-
if mod_name == ""9290-
return9291-
end9292-
eff_prefix = module_prefix9293-
if const_ref_is_relative(inc_nid) == 09294-
eff_prefix = ""9295-
end9296-
resolved_mod = resolve_include_module_name(mod_name, eff_prefix)9297-
mi = 09298-
while mi < @module_names.length9299-
if @module_names[mi] == resolved_mod9300-
mbody = @module_body_ids[mi]9301-
if mbody >= 09302-
mstmts = get_stmts(mbody)9303-
mk = 09304-
while mk < mstmts.length9305-
sid = mstmts[mk]9306-
if @nd_type[sid] == "DefNode"9307-
is_cls_def = 09308-
if @nd_receiver[sid] >= 0 && @nd_type[@nd_receiver[sid]] == "SelfNode"9309-
is_cls_def = 19310-
end9311-
if is_cls_def == 09312-
ocfull = "__oc_" + cname + "_" + @nd_name[sid]9313-
if not_in(ocfull, @meth_names) == 19314-
register_open_class_def(cname, @nd_name[sid], sid)9315-
end9316-
end9322+
if @nd_type[nid] == "CallNode" && @nd_name[nid] == "new"9323+
recv = @nd_receiver[nid]9324+
if recv >= 09325+
cname = constructor_class_name(recv)9326+
if cname != ""9327+
ci = find_class_idx(cname)9328+
if ci >= 09329+
args_id = @nd_arguments[nid]9330+
argc = 09331+
if args_id >= 09332+
argc = get_args(args_id).length9333+
end9334+
if argc == 09335+
site_ids.push(nid)9336+
site_classes.push(cname)93179337
end9318-
mk = mk + 193199338
end93209339
end93219340
end9322-
mi = mi + 193239341
end9342+
cs = []9343+
push_child_ids(nid, cs)9344+
k = 09345+
while k < cs.length9346+
collect_implicit_new_site_candidates(cs[k], site_ids, site_classes)9347+
k = k + 19348+
end93249349
end93259350
9326-
def collect_class_with_prefix(nid, module_prefix)9327-
ci = @cls_names.length9328-
cname = ""9329-
cp = @nd_constant_path[nid]9330-
if cp >= 09331-
cname = const_ref_flat_name(cp)9332-
# For `module M; class C; ... end; end`, Prism gives class name as9333-
# ConstantReadNode("C"), so attach lexical module prefix.9334-
if module_prefix != "" && const_ref_is_relative(cp) == 19335-
cname = module_prefix + "_" + cname9351+
def count_implicit_new_sites_for_class(site_classes, cname)9352+
count = 09353+
k = 09354+
while k < site_classes.length9355+
if site_classes[k] == cname9356+
count = count + 193369357
end9358+
k = k + 193379359
end9360+
count9361+
end93389362
9339-
# Check for open class on built-in type9340-
if is_builtin_type_name(cname) == 19341-
@open_class_names.push(cname)9342-
# Collect methods as top-level functions with special naming9343-
body = @nd_body[nid]9344-
if body >= 09345-
body_stmts = get_stmts(body)9346-
body_stmts.each { |sid|9347-
if @nd_type[sid] == "DefNode"9348-
register_open_class_def(cname, @nd_name[sid], sid)9349-
end9350-
# `include M` inside a reopened built-in: the module's instance9351-
# methods must also become `__oc_<cname>_<m>` top-level functions so9352-
# primitive dispatch (e.g. `42.to_words`) finds them, exactly like a9353-
# direct `def`. Without this the include was silently dropped here.9354-
if @nd_type[sid] == "CallNode" && @nd_name[sid] == "include"9355-
inc_args_oc = @nd_arguments[sid]9356-
if inc_args_oc >= 09357-
inc_ids_oc = get_args(inc_args_oc)9358-
iko = 09359-
while iko < inc_ids_oc.length9360-
register_open_class_module_methods(cname, inc_ids_oc[iko], module_prefix)9361-
iko = iko + 19362-
end9363-
end9364-
end9365-
}9366-
end9367-
return9363+
def implicit_candidate_new_class(nid)9364+
if nid < 0 || @nd_type[nid] != "CallNode" || @nd_name[nid] != "new"9365+
return ""93689366
end9369-
9370-
# Class reopening: if the class was already registered (in an9371-
# earlier `class Foo ... end` block), reuse the existing entry9372-
# so methods and attrs from this body get appended rather than9367+
recv = @nd_receiver[nid]9368+
if recv < 09369+
return ""9370+
end9371+
cname = constructor_class_name(recv)9372+
if cname == ""9373+
return ""9374+
end9375+
ci = find_class_idx(cname)9376+
if ci < 0 || implicit_specializable_class?(ci) == 09377+
return ""9378+
end9379+
args_id = @nd_arguments[nid]9380+
if args_id >= 0 && get_args(args_id).length > 09381+
return ""9382+
end9383+
cname9384+
end9385+
9386+
def implicit_candidate_local_class(name, local_names, local_classes)9387+
found = ""9388+
k = 09389+
while k < local_names.length9390+
if local_names[k] == name9391+
cls = local_classes[k]9392+
if found == ""9393+
found = cls9394+
elsif found != cls9395+
return ""9396+
end9397+
end9398+
k = k + 19399+
end9400+
found9401+
end9402+
9403+
def collect_implicit_candidate_local_bindings_in_scope(nid, local_names, local_classes)9404+
if nid < 09405+
return9406+
end9407+
t = @nd_type[nid]9408+
if t == "DefNode" || t == "ClassNode" || t == "ModuleNode" || t == "SingletonClassNode"9409+
return9410+
end9411+
if t == "LocalVariableWriteNode"9412+
cname = implicit_candidate_new_class(@nd_expression[nid])9413+
if cname != ""9414+
local_names.push(@nd_name[nid])9415+
local_classes.push(cname)9416+
end9417+
end9418+
cs = []9419+
push_child_ids(nid, cs)9420+
k = 09421+
while k < cs.length9422+
collect_implicit_candidate_local_bindings_in_scope(cs[k], local_names, local_classes)9423+
k = k + 19424+
end9425+
end9426+
9427+
def mark_implicit_candidate_escape(cname, escaping_classes)9428+
if cname == ""9429+
return9430+
end9431+
k = 09432+
while k < escaping_classes.length9433+
if escaping_classes[k] == cname9434+
return9435+
end9436+
k = k + 19437+
end9438+
escaping_classes.push(cname)9439+
end9440+
9441+
def mark_implicit_candidate_expr_escape(nid, local_names, local_classes, escaping_classes)9442+
if nid < 09443+
return9444+
end9445+
t = @nd_type[nid]9446+
if t == "DefNode" || t == "ClassNode" || t == "ModuleNode" || t == "SingletonClassNode"9447+
return9448+
end9449+
cname = implicit_candidate_new_class(nid)9450+
if cname != ""9451+
mark_implicit_candidate_escape(cname, escaping_classes)9452+
return9453+
end9454+
if t == "LocalVariableReadNode"9455+
mark_implicit_candidate_escape(implicit_candidate_local_class(@nd_name[nid], local_names, local_classes), escaping_classes)9456+
return9457+
end9458+
if t == "CallNode"9459+
args_id = @nd_arguments[nid]9460+
if args_id >= 09461+
arg_ids = get_args(args_id)9462+
k = 09463+
while k < arg_ids.length9464+
mark_implicit_candidate_expr_escape(arg_ids[k], local_names, local_classes, escaping_classes)9465+
k = k + 19466+
end9467+
end9468+
return9469+
end9470+
cs = []9471+
push_child_ids(nid, cs)9472+
k = 09473+
while k < cs.length9474+
mark_implicit_candidate_expr_escape(cs[k], local_names, local_classes, escaping_classes)9475+
k = k + 19476+
end9477+
end9478+
9479+
def collect_implicit_candidate_escapes_in_scope(nid, local_names, local_classes, escaping_classes)9480+
if nid < 09481+
return9482+
end9483+
t = @nd_type[nid]9484+
if t == "DefNode" || t == "ClassNode" || t == "ModuleNode" || t == "SingletonClassNode"9485+
return9486+
end9487+
if t == "CallNode"9488+
args_id = @nd_arguments[nid]9489+
if args_id >= 09490+
arg_ids = get_args(args_id)9491+
k = 09492+
while k < arg_ids.length9493+
mark_implicit_candidate_expr_escape(arg_ids[k], local_names, local_classes, escaping_classes)9494+
k = k + 19495+
end9496+
end9497+
elsif t == "ReturnNode"9498+
args_id = @nd_arguments[nid]9499+
if args_id >= 09500+
arg_ids = get_args(args_id)9501+
k = 09502+
while k < arg_ids.length9503+
mark_implicit_candidate_expr_escape(arg_ids[k], local_names, local_classes, escaping_classes)9504+
k = k + 19505+
end9506+
end9507+
elsif t == "ArrayNode" || t == "HashNode" || t == "KeywordHashNode"9508+
mark_implicit_candidate_expr_escape(nid, local_names, local_classes, escaping_classes)9509+
return9510+
elsif t == "InstanceVariableWriteNode" || t == "GlobalVariableWriteNode" || t == "ClassVariableWriteNode" || t == "ConstantWriteNode"9511+
mark_implicit_candidate_expr_escape(@nd_expression[nid], local_names, local_classes, escaping_classes)9512+
end9513+
cs = []9514+
push_child_ids(nid, cs)9515+
k = 09516+
while k < cs.length9517+
collect_implicit_candidate_escapes_in_scope(cs[k], local_names, local_classes, escaping_classes)9518+
k = k + 19519+
end9520+
end9521+
9522+
def collect_implicit_candidate_escape_scope(nid, escaping_classes)9523+
if nid < 09524+
return9525+
end9526+
local_names = "".split(",", -1)9527+
local_classes = "".split(",", -1)9528+
collect_implicit_candidate_local_bindings_in_scope(nid, local_names, local_classes)9529+
collect_implicit_candidate_escapes_in_scope(nid, local_names, local_classes, escaping_classes)9530+
end9531+
9532+
def collect_nested_implicit_candidate_escape_scopes(nid, escaping_classes)9533+
if nid < 09534+
return9535+
end9536+
cs = []9537+
push_child_ids(nid, cs)9538+
k = 09539+
while k < cs.length9540+
child = cs[k]9541+
t = @nd_type[child]9542+
if t == "DefNode" || t == "ClassNode" || t == "ModuleNode"9543+
collect_implicit_candidate_escapes_by_scope(@nd_body[child], escaping_classes)9544+
elsif t == "SingletonClassNode"9545+
collect_implicit_candidate_escapes_by_scope(@nd_body[child], escaping_classes)9546+
else9547+
collect_nested_implicit_candidate_escape_scopes(child, escaping_classes)9548+
end9549+
k = k + 19550+
end9551+
end9552+
9553+
def collect_implicit_candidate_escapes_by_scope(nid, escaping_classes)9554+
if nid < 09555+
return9556+
end9557+
collect_implicit_candidate_escape_scope(nid, escaping_classes)9558+
collect_nested_implicit_candidate_escape_scopes(nid, escaping_classes)9559+
end9560+
9561+
def implicit_candidate_class_escaped?(escaping_classes, cname)9562+
k = 09563+
while k < escaping_classes.length9564+
if escaping_classes[k] == cname9565+
return 19566+
end9567+
k = k + 19568+
end9569+
09570+
end9571+
9572+
def copy_cls_rest_entries_for_implicit_specialization(old_name, new_name)9573+
prefix = old_name + "#"9574+
plen = prefix.length9575+
k = 09576+
while k < @cls_rest_keys.length9577+
key = @cls_rest_keys[k]9578+
if key.length > plen && key[0, plen] == prefix9579+
@cls_rest_keys.push(new_name + "#" + key[plen, key.length - plen])9580+
@cls_rest_idxs.push(@cls_rest_idxs[k])9581+
end9582+
k = k + 19583+
end9584+
end9585+
9586+
def clone_class_slot_for_implicit_new(template_ci, spec_name)9587+
old_name = @cls_names[template_ci]9588+
@cls_names.push(spec_name)9589+
@cls_parents.push(@cls_parents[template_ci])9590+
@cls_includes.push(@cls_includes[template_ci])9591+
@cls_ivar_names.push(@cls_ivar_names[template_ci])9592+
@cls_ivar_types.push(@cls_ivar_types[template_ci])9593+
@cls_ivar_init_definite.push(@cls_ivar_init_definite[template_ci])9594+
@cls_ivar_observed_types.push(@cls_ivar_observed_types[template_ci])9595+
@cls_ivar_rbs_types.push(@cls_ivar_rbs_types[template_ci])9596+
@cls_ivar_nil_checked.push(@cls_ivar_nil_checked[template_ci])9597+
@cls_meth_names.push(@cls_meth_names[template_ci])9598+
@cls_meth_params.push(@cls_meth_params[template_ci])9599+
@cls_meth_ptypes.push(@cls_meth_ptypes[template_ci])9600+
@cls_meth_returns.push(@cls_meth_returns[template_ci])9601+
@cls_meth_bodies.push(@cls_meth_bodies[template_ci])9602+
@cls_meth_defaults.push(@cls_meth_defaults[template_ci])9603+
@cls_meth_ptypes_empty.push(@cls_meth_ptypes_empty[template_ci])9604+
@cls_meth_prep_chain.push(@cls_meth_prep_chain[template_ci])9605+
@cls_attr_readers.push(@cls_attr_readers[template_ci])9606+
@cls_attr_writers.push(@cls_attr_writers[template_ci])9607+
@cls_cmeth_names.push(@cls_cmeth_names[template_ci])9608+
@cls_cmeth_params.push(@cls_cmeth_params[template_ci])9609+
@cls_cmeth_ptypes.push(@cls_cmeth_ptypes[template_ci])9610+
@cls_cmeth_returns.push(@cls_cmeth_returns[template_ci])9611+
@cls_cmeth_bodies.push(@cls_cmeth_bodies[template_ci])9612+
@cls_cmeth_defaults.push(@cls_cmeth_defaults[template_ci])9613+
@cls_cmeth_scope_names.push(@cls_cmeth_scope_names[template_ci])9614+
@cls_cmeth_scope_types.push(@cls_cmeth_scope_types[template_ci])9615+
@cls_is_value_type.push(@cls_is_value_type[template_ci])9616+
@cls_is_sra.push(@cls_is_sra[template_ci])9617+
@cls_meth_has_yield.push(@cls_meth_has_yield[template_ci])9618+
copy_cls_rest_entries_for_implicit_specialization(old_name, spec_name)9619+
@cls_ivar_types_version = @cls_ivar_types_version + 19620+
@cls_meth_ptypes_version = @cls_meth_ptypes_version + 19621+
@cls_meth_params_version = @cls_meth_params_version + 19622+
@cls_cmeth_ptypes_version = @cls_cmeth_ptypes_version + 19623+
@cls_cmeth_params_version = @cls_cmeth_params_version + 19624+
end9625+
9626+
def collect_implicit_new_local_bindings(nid)9627+
if nid < 09628+
return9629+
end9630+
if @nd_type[nid] == "LocalVariableWriteNode"9631+
expr = @nd_expression[nid]9632+
cls = implicit_new_site_class(expr)9633+
if cls != ""9634+
@implicit_new_local_names.push(@nd_name[nid])9635+
@implicit_new_local_classes.push(cls)9636+
end9637+
end9638+
cs = []9639+
push_child_ids(nid, cs)9640+
k = 09641+
while k < cs.length9642+
collect_implicit_new_local_bindings(cs[k])9643+
k = k + 19644+
end9645+
end9646+
9647+
def rewrite_implicit_new_ivar_init_type(ci, iname, spec_name)9648+
if ci < 0 || ci >= @cls_names.length9649+
return9650+
end9651+
names = @cls_ivar_names[ci].split(";", -1)9652+
types = @cls_ivar_types[ci].split(";", -1)9653+
changed = 09654+
k = 09655+
while k < names.length9656+
if names[k] == iname9657+
spec_t = "obj_" + spec_name9658+
base_name = spec_name9659+
marker = "__implicit_"9660+
mark_idx = base_name.index(marker)9661+
if mark_idx9662+
base_name = base_name[0, mark_idx]9663+
end9664+
base_t = "obj_" + base_name9665+
if k >= types.length9666+
while types.length <= k9667+
types.push("int")9668+
end9669+
end9670+
if types[k] == base_t || types[k] == "int"9671+
types[k] = spec_t9672+
changed = 19673+
end9674+
end9675+
k = k + 19676+
end9677+
if changed == 19678+
@cls_ivar_types[ci] = types.join(";")9679+
@cls_ivar_types_version = @cls_ivar_types_version + 19680+
end9681+
end9682+
9683+
def normalize_implicit_new_ivar_inits(nid, class_idx, module_prefix)9684+
if nid < 09685+
return9686+
end9687+
t = @nd_type[nid]9688+
if t == "ModuleNode"9689+
mname = ""9690+
cp = @nd_constant_path[nid]9691+
if cp >= 09692+
mname = const_ref_flat_name(cp)9693+
if module_prefix != "" && const_ref_is_relative(cp) == 19694+
mname = module_prefix + "_" + mname9695+
end9696+
end9697+
normalize_implicit_new_ivar_inits(@nd_body[nid], -1, mname)9698+
return9699+
end9700+
if t == "ClassNode"9701+
cname = ""9702+
cp = @nd_constant_path[nid]9703+
if cp >= 09704+
cname = const_ref_flat_name(cp)9705+
if module_prefix != "" && const_ref_is_relative(cp) == 19706+
cname = module_prefix + "_" + cname9707+
end9708+
end9709+
ci = find_class_idx(cname)9710+
normalize_implicit_new_ivar_inits(@nd_body[nid], ci, module_prefix)9711+
return9712+
end9713+
if t == "SingletonClassNode"9714+
return9715+
end9716+
if t == "InstanceVariableWriteNode" && class_idx >= 09717+
spec_cls = implicit_new_site_class(@nd_expression[nid])9718+
if spec_cls != ""9719+
rewrite_implicit_new_ivar_init_type(class_idx, @nd_name[nid], spec_cls)9720+
end9721+
end9722+
cs = []9723+
push_child_ids(nid, cs)9724+
k = 09725+
while k < cs.length9726+
normalize_implicit_new_ivar_inits(cs[k], class_idx, module_prefix)9727+
k = k + 19728+
end9729+
end9730+
9731+
def collect_implicit_new_specializations9732+
site_ids = []9733+
site_classes = "".split(",", -1)9734+
collect_implicit_new_site_candidates(@root_id, site_ids, site_classes)9735+
escaping_classes = "".split(",", -1)9736+
collect_implicit_candidate_escapes_by_scope(@root_id, escaping_classes)9737+
original_class_count = @cls_names.length9738+
ci = 09739+
while ci < original_class_count9740+
cname = @cls_names[ci]9741+
if implicit_specializable_class?(ci) == 1 && implicit_candidate_class_escaped?(escaping_classes, cname) == 0 && count_implicit_new_sites_for_class(site_classes, cname) >= 29742+
k = 09743+
while k < site_ids.length9744+
if site_classes[k] == cname9745+
spec_name = cname + "__implicit_" + site_ids[k].to_s9746+
if find_class_idx(spec_name) < 09747+
clone_class_slot_for_implicit_new(ci, spec_name)9748+
end9749+
@implicit_new_site_ids.push(site_ids[k])9750+
@implicit_new_site_classes.push(spec_name)9751+
end9752+
k = k + 19753+
end9754+
end9755+
ci = ci + 19756+
end9757+
collect_implicit_new_local_bindings(@root_id)9758+
normalize_implicit_new_ivar_inits(@root_id, -1, "")9759+
end9760+
9761+
def collect_scoped_constant(scope_name, nid)9762+
cname = @nd_name[nid]9763+
if scope_name != ""9764+
cname = scope_name + "_" + cname9765+
end9766+
expr_id = @nd_expression[nid]9767+
if expr_id >= 09768+
# `Foo = Struct.new(:a, :b)` and `Foo = Data.define(:a, :b)` share9769+
# a structural contract: positional symbol args naming the fields.9770+
# Data is keyword-init at runtime (`Foo.new(a: 1, b: 2)`), which9771+
# the existing kwarg constructor path already matches by name.9772+
# Routing both through `collect_struct_class` registers the9773+
# synthetic class so codegen has a `sp_<Foo>` type to emit;9774+
# without this the AST-emitted `obj_<Foo>` reference resolves to9775+
# an undeclared struct, tripping clang `unknown type name9776+
# 'sp_<Foo>'`.9777+
if is_call_on_const(expr_id, "Struct", "new") == 1 || is_call_on_const(expr_id, "Data", "define") == 19778+
collect_struct_class(cname, expr_id)9779+
return9780+
end9781+
end9782+
ct = "int"9783+
if expr_id >= 09784+
old_scope = @current_lexical_scope9785+
@current_lexical_scope = scope_name9786+
ct = infer_type(expr_id)9787+
@current_lexical_scope = old_scope9788+
end9789+
ci = find_const_idx(cname)9790+
if ci >= 09791+
@const_types[ci] = ct9792+
@const_expr_ids[ci] = expr_id9793+
@const_scope_names[ci] = scope_name9794+
return9795+
end9796+
@const_names.push(cname)9797+
@const_types.push(ct)9798+
@const_expr_ids.push(expr_id)9799+
@const_scope_names.push(scope_name)9800+
end9801+
9802+
# Register a single method as an open-class top-level function9803+
# (`__oc_<cname>_<mname>`) -- the form primitive dispatch resolves for9804+
# built-in reopens (`class Integer; def to_words`). Shared by the9805+
# direct-def and include-into-built-in paths.9806+
def register_open_class_def(cname, mname, sid)9807+
@meth_names.push("__oc_" + cname + "_" + mname)9808+
@meth_param_names.push(collect_params_str(sid))9809+
@meth_param_types.push(collect_ptypes_str(sid, -1))9810+
@meth_param_empty.push("")9811+
@meth_return_types.push("int")9812+
@meth_body_ids.push(@nd_body[sid])9813+
@meth_has_yield.push(0)9814+
@meth_has_defaults.push("0")9815+
@meth_rest_index.push(collect_rest_index(sid))9816+
@meth_kwrest_index.push(collect_kwrest_index(sid))9817+
end9818+
9819+
# Register the instance methods of an included module as open-class9820+
# functions on a reopened built-in (`class Integer; include M`). Skips9821+
# `def self.x` module functions and names already registered (so a9822+
# direct def or a second include doesn't duplicate the C function).9823+
def register_open_class_module_methods(cname, inc_nid, module_prefix)9824+
inc_t = @nd_type[inc_nid]9825+
if inc_t != "ConstantReadNode" && inc_t != "ConstantPathNode"9826+
return9827+
end9828+
mod_name = const_ref_flat_name(inc_nid)9829+
if mod_name == ""9830+
return9831+
end9832+
eff_prefix = module_prefix9833+
if const_ref_is_relative(inc_nid) == 09834+
eff_prefix = ""9835+
end9836+
resolved_mod = resolve_include_module_name(mod_name, eff_prefix)9837+
mi = 09838+
while mi < @module_names.length9839+
if @module_names[mi] == resolved_mod9840+
mbody = @module_body_ids[mi]9841+
if mbody >= 09842+
mstmts = get_stmts(mbody)9843+
mk = 09844+
while mk < mstmts.length9845+
sid = mstmts[mk]9846+
if @nd_type[sid] == "DefNode"9847+
is_cls_def = 09848+
if @nd_receiver[sid] >= 0 && @nd_type[@nd_receiver[sid]] == "SelfNode"9849+
is_cls_def = 19850+
end9851+
if is_cls_def == 09852+
ocfull = "__oc_" + cname + "_" + @nd_name[sid]9853+
if not_in(ocfull, @meth_names) == 19854+
register_open_class_def(cname, @nd_name[sid], sid)9855+
end9856+
end9857+
end9858+
mk = mk + 19859+
end9860+
end9861+
end9862+
mi = mi + 19863+
end9864+
end9865+
9866+
def collect_class_with_prefix(nid, module_prefix)9867+
ci = @cls_names.length9868+
cname = ""9869+
cp = @nd_constant_path[nid]9870+
if cp >= 09871+
cname = const_ref_flat_name(cp)9872+
# For `module M; class C; ... end; end`, Prism gives class name as9873+
# ConstantReadNode("C"), so attach lexical module prefix.9874+
if module_prefix != "" && const_ref_is_relative(cp) == 19875+
cname = module_prefix + "_" + cname9876+
end9877+
end9878+
9879+
# Check for open class on built-in type9880+
if is_builtin_type_name(cname) == 19881+
@open_class_names.push(cname)9882+
# Collect methods as top-level functions with special naming9883+
body = @nd_body[nid]9884+
if body >= 09885+
body_stmts = get_stmts(body)9886+
body_stmts.each { |sid|9887+
if @nd_type[sid] == "DefNode"9888+
register_open_class_def(cname, @nd_name[sid], sid)9889+
end9890+
# `include M` inside a reopened built-in: the module's instance9891+
# methods must also become `__oc_<cname>_<m>` top-level functions so9892+
# primitive dispatch (e.g. `42.to_words`) finds them, exactly like a9893+
# direct `def`. Without this the include was silently dropped here.9894+
if @nd_type[sid] == "CallNode" && @nd_name[sid] == "include"9895+
inc_args_oc = @nd_arguments[sid]9896+
if inc_args_oc >= 09897+
inc_ids_oc = get_args(inc_args_oc)9898+
iko = 09899+
while iko < inc_ids_oc.length9900+
register_open_class_module_methods(cname, inc_ids_oc[iko], module_prefix)9901+
iko = iko + 19902+
end9903+
end9904+
end9905+
}9906+
end9907+
return9908+
end9909+
9910+
# Class reopening: if the class was already registered (in an9911+
# earlier `class Foo ... end` block), reuse the existing entry9912+
# so methods and attrs from this body get appended rather than93739913
# producing a duplicate C struct/constructor.93749914
existing_ci = find_class_idx(cname)93759915
if existing_ci >= 0@@ -14719,7 +15259,13 @@
1471915259
# `0` arg and collapse it to poly.1472015260
recv_iow_fwd = @nd_receiver[nid]1472115261
recv_is_ivar_or_local = recv_iow_fwd >= 0 && (@nd_type[recv_iow_fwd] == "InstanceVariableReadNode" || @nd_type[recv_iow_fwd] == "LocalVariableReadNode")14722-
if rt == "int" && recv_is_ivar_or_local && primitive_method_shared_with_user_class(mname) == 015262+
recv_has_implicit_new_class = 015263+
if recv_iow_fwd >= 0 && @nd_type[recv_iow_fwd] == "LocalVariableReadNode"15264+
if implicit_new_local_class(@nd_name[recv_iow_fwd]) != ""15265+
recv_has_implicit_new_class = 115266+
end15267+
end15268+
if rt == "int" && recv_is_ivar_or_local && recv_has_implicit_new_class == 0 && primitive_method_shared_with_user_class(mname) == 01472315269
# Walk every user class that defines mname AND whose1472415270
# param count matches the call's arg count. Pre-fix1472515271
# this branch bailed on multi-match casesspinel_codegen.rbmodifiedView file
Loading diff…
test/implicit_alloc_site_cache_helper_specialization.rbaddedView file
Loading diff…
test/implicit_alloc_site_cache_helper_specialization.rb.expectedaddedView file
Loading diff…
test/implicit_alloc_site_escape_layout.rbaddedView file
Loading diff…
test/implicit_alloc_site_escape_layout.rb.expectedaddedView file
Loading diff…
test/implicit_alloc_site_escape_param.rbaddedView file
Loading diff…
test/implicit_alloc_site_escape_param.rb.expectedaddedView file
Loading diff…
test/implicit_alloc_site_escape_scoped_locals.rbaddedView file
Loading diff…
test/implicit_alloc_site_escape_scoped_locals.rb.expectedaddedView file
Loading diff…
test/implicit_alloc_site_store_specialization.rbaddedView file
Loading diff…
test/implicit_alloc_site_store_specialization.rb.expectedaddedView file
Loading diff…