Commit 2702b38e
2702b38e73cfMerged
sacckey <[email protected]> · 7 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): support module-nested classes and ConstantPath .new
Details
- Commit
- 2702b38e73cf
- Parent
- 9bf488830f19
spinel_codegen.rbmodifiedView file
@@ -683,6 +683,45 @@
683683
result684684
end685685
686+
# Flatten a constant reference into an internal name.687+
# C -> C688+
# ::C -> C689+
# M::C -> M_C690+
# A::B::C -> A_B_C691+
def const_ref_flat_name(nid)692+
if nid < 0693+
return ""694+
end695+
t = @nd_type[nid]696+
if t == "ConstantReadNode"697+
return @nd_name[nid]698+
end699+
if t == "ConstantPathNode"700+
leaf = @nd_name[nid]701+
parent = @nd_receiver[nid]702+
if parent < 0703+
return leaf704+
end705+
base = const_ref_flat_name(parent)706+
if base == ""707+
return leaf708+
end709+
return base + "_" + leaf710+
end711+
""712+
end713+
714+
def constructor_class_name(recv_nid)715+
if recv_nid < 0716+
return ""717+
end718+
rt = @nd_type[recv_nid]719+
if rt == "ConstantReadNode" || rt == "ConstantPathNode"720+
return const_ref_flat_name(recv_nid)721+
end722+
""723+
end724+
686725
# ---- Scope management ----687726
def push_scope688727
@scope_names.push("---")@@ -2409,9 +2448,9 @@
24092448
def infer_constructor_type(nid, mname, recv)24102449
if mname == "new"24112450
if recv >= 02412-
if @nd_type[recv] == "ConstantReadNode"2413-
rn = @nd_name[recv]2414-
if rn == "Array"2451+
rn = constructor_class_name(recv)2452+
if rn != ""2453+
if @nd_type[recv] == "ConstantReadNode" && rn == "Array"24152454
# Check fill value type24162455
args_id = @nd_arguments[nid]24172456
if args_id >= 0@@ -2428,16 +2467,16 @@
24282467
end24292468
return "int_array"24302469
end2431-
if rn == "Hash"2470+
if @nd_type[recv] == "ConstantReadNode" && rn == "Hash"24322471
return "str_int_hash"24332472
end2434-
if rn == "Proc"2473+
if @nd_type[recv] == "ConstantReadNode" && rn == "Proc"24352474
return "proc"24362475
end2437-
if rn == "StringIO"2476+
if @nd_type[recv] == "ConstantReadNode" && rn == "StringIO"24382477
return "stringio"24392478
end2440-
if rn == "Fiber"2479+
if @nd_type[recv] == "ConstantReadNode" && rn == "Fiber"24412480
return "fiber"24422481
end24432482
return "obj_" + rn@@ -2480,9 +2519,9 @@
24802519
end24812520
# User-defined class methods24822521
if recv >= 02483-
if @nd_type[recv] == "ConstantReadNode"2484-
rcname = @nd_name[recv]2485-
if rcname == "Fiber"2522+
rcname = constructor_class_name(recv)2523+
if rcname != ""2524+
if @nd_type[recv] == "ConstantReadNode" && rcname == "Fiber"24862525
if mname == "new"24872526
return "fiber"24882527
end@@ -3338,11 +3377,20 @@
33383377
end33393378
33403379
def collect_class(nid)3380+
collect_class_with_prefix(nid, "")3381+
end3382+
3383+
def collect_class_with_prefix(nid, module_prefix)33413384
ci = @cls_names.length33423385
cname = ""33433386
cp = @nd_constant_path[nid]33443387
if cp >= 03345-
cname = @nd_name[cp]3388+
cname = const_ref_flat_name(cp)3389+
# For `module M; class C; ... end; end`, Prism gives class name as3390+
# ConstantReadNode("C"), so attach lexical module prefix.3391+
if module_prefix != "" && @nd_type[cp] == "ConstantReadNode"3392+
cname = module_prefix + "_" + cname3393+
end33463394
end33473395
33483396
# Check for open class on built-in type@@ -4116,12 +4164,12 @@
41164164
if mname == "new"41174165
r = @nd_receiver[nid]41184166
if r >= 04119-
if @nd_type[r] == "ConstantReadNode"4120-
rname = @nd_name[r]4121-
if rname == "Array"4167+
rname = constructor_class_name(r)4168+
if rname != ""4169+
if @nd_type[r] == "ConstantReadNode" && rname == "Array"41224170
return "int_array"41234171
end4124-
if rname == "Hash"4172+
if @nd_type[r] == "ConstantReadNode" && rname == "Hash"41254173
return "str_int_hash"41264174
end41274175
if rname == "StringIO"@@ -4269,10 +4317,17 @@
42694317
end42704318
42714319
def collect_module(nid)4320+
collect_module_with_prefix(nid, "")4321+
end4322+
4323+
def collect_module_with_prefix(nid, module_prefix)42724324
mname = ""42734325
cp = @nd_constant_path[nid]42744326
if cp >= 04275-
mname = @nd_name[cp]4327+
mname = const_ref_flat_name(cp)4328+
if module_prefix != "" && @nd_type[cp] == "ConstantReadNode"4329+
mname = module_prefix + "_" + mname4330+
end42764331
end42774332
body = @nd_body[nid]42784333
# Store module info for include@@ -4282,6 +4337,19 @@
42824337
return42834338
end42844339
body_stmts = get_stmts(body)4340+
4341+
# Match top-level collection order: modules first, then classes.4342+
body_stmts.each { |sid|4343+
if @nd_type[sid] == "ModuleNode"4344+
collect_module_with_prefix(sid, mname)4345+
end4346+
}4347+
body_stmts.each { |sid|4348+
if @nd_type[sid] == "ClassNode"4349+
collect_class_with_prefix(sid, mname)4350+
end4351+
}4352+
42854353
body_stmts.each { |sid|42864354
if @nd_type[sid] == "ConstantWriteNode"42874355
cname = mname + "_" + @nd_name[sid]@@ -4673,8 +4741,8 @@
46734741
if @nd_name[nid] == "new"46744742
recv = @nd_receiver[nid]46754743
if recv >= 04676-
if @nd_type[recv] == "ConstantReadNode"4677-
cname = @nd_name[recv]4744+
cname = constructor_class_name(recv)4745+
if cname != ""46784746
ci = find_class_idx(cname)46794747
if ci >= 046804748
init_ci = find_init_class(ci)@@ -13177,15 +13245,15 @@
1317713245
end1317813246
1317913247
def compile_constructor_expr(nid, recv)13180-
if @nd_type[recv] == "ConstantReadNode"13181-
cname = @nd_name[recv]13182-
if cname == "Proc"13248+
cname = constructor_class_name(recv)13249+
if cname != ""13250+
if @nd_type[recv] == "ConstantReadNode" && cname == "Proc"1318313251
if @nd_block[nid] >= 01318413252
@needs_proc = 11318513253
return compile_proc_literal(nid)1318613254
end1318713255
end13188-
if cname == "Array"13256+
if @nd_type[recv] == "ConstantReadNode" && cname == "Array"1318913257
@needs_gc = 11319013258
args_id = @nd_arguments[nid]1319113259
if args_id >= 0@@ -13217,7 +13285,7 @@
1321713285
@needs_int_array = 11321813286
return "sp_IntArray_new()"1321913287
end13220-
if cname == "Hash"13288+
if @nd_type[recv] == "ConstantReadNode" && cname == "Hash"1322113289
@needs_str_int_hash = 11322213290
@needs_gc = 11322313291
args_id = @nd_arguments[nid]@@ -13237,7 +13305,7 @@
1323713305
end1323813306
return "sp_StrIntHash_new()"1323913307
end13240-
if cname == "StringIO"13308+
if @nd_type[recv] == "ConstantReadNode" && cname == "StringIO"1324113309
@needs_stringio = 11324213310
args_id = @nd_arguments[nid]1324313311
if args_id >= 0test/bm_constant_path_new.rbaddedView file
Loading diff…
test/bm_module_class_new.rbaddedView file
Loading diff…