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 result
684684 end
685685
686+ # Flatten a constant reference into an internal name.
687+ # C -> C
688+ # ::C -> C
689+ # M::C -> M_C
690+ # A::B::C -> A_B_C
691+ def const_ref_flat_name(nid)
692+ if nid < 0
693+ return ""
694+ end
695+ t = @nd_type[nid]
696+ if t == "ConstantReadNode"
697+ return @nd_name[nid]
698+ end
699+ if t == "ConstantPathNode"
700+ leaf = @nd_name[nid]
701+ parent = @nd_receiver[nid]
702+ if parent < 0
703+ return leaf
704+ end
705+ base = const_ref_flat_name(parent)
706+ if base == ""
707+ return leaf
708+ end
709+ return base + "_" + leaf
710+ end
711+ ""
712+ end
713+
714+ def constructor_class_name(recv_nid)
715+ if recv_nid < 0
716+ return ""
717+ end
718+ rt = @nd_type[recv_nid]
719+ if rt == "ConstantReadNode" || rt == "ConstantPathNode"
720+ return const_ref_flat_name(recv_nid)
721+ end
722+ ""
723+ end
724+
686725 # ---- Scope management ----
687726 def push_scope
688727 @scope_names.push("---")
@@ -2409,9 +2448,9 @@
24092448 def infer_constructor_type(nid, mname, recv)
24102449 if mname == "new"
24112450 if recv >= 0
2412- 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 type
24162455 args_id = @nd_arguments[nid]
24172456 if args_id >= 0
@@ -2428,16 +2467,16 @@
24282467 end
24292468 return "int_array"
24302469 end
2431- if rn == "Hash"
2470+ if @nd_type[recv] == "ConstantReadNode" && rn == "Hash"
24322471 return "str_int_hash"
24332472 end
2434- if rn == "Proc"
2473+ if @nd_type[recv] == "ConstantReadNode" && rn == "Proc"
24352474 return "proc"
24362475 end
2437- if rn == "StringIO"
2476+ if @nd_type[recv] == "ConstantReadNode" && rn == "StringIO"
24382477 return "stringio"
24392478 end
2440- if rn == "Fiber"
2479+ if @nd_type[recv] == "ConstantReadNode" && rn == "Fiber"
24412480 return "fiber"
24422481 end
24432482 return "obj_" + rn
@@ -2480,9 +2519,9 @@
24802519 end
24812520 # User-defined class methods
24822521 if recv >= 0
2483- 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 end
33393378
33403379 def collect_class(nid)
3380+ collect_class_with_prefix(nid, "")
3381+ end
3382+
3383+ def collect_class_with_prefix(nid, module_prefix)
33413384 ci = @cls_names.length
33423385 cname = ""
33433386 cp = @nd_constant_path[nid]
33443387 if cp >= 0
3345- cname = @nd_name[cp]
3388+ cname = const_ref_flat_name(cp)
3389+ # For `module M; class C; ... end; end`, Prism gives class name as
3390+ # ConstantReadNode("C"), so attach lexical module prefix.
3391+ if module_prefix != "" && @nd_type[cp] == "ConstantReadNode"
3392+ cname = module_prefix + "_" + cname
3393+ end
33463394 end
33473395
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 >= 0
4119- 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 end
4124- if rname == "Hash"
4172+ if @nd_type[r] == "ConstantReadNode" && rname == "Hash"
41254173 return "str_int_hash"
41264174 end
41274175 if rname == "StringIO"
@@ -4269,10 +4317,17 @@
42694317 end
42704318
42714319 def collect_module(nid)
4320+ collect_module_with_prefix(nid, "")
4321+ end
4322+
4323+ def collect_module_with_prefix(nid, module_prefix)
42724324 mname = ""
42734325 cp = @nd_constant_path[nid]
42744326 if cp >= 0
4275- mname = @nd_name[cp]
4327+ mname = const_ref_flat_name(cp)
4328+ if module_prefix != "" && @nd_type[cp] == "ConstantReadNode"
4329+ mname = module_prefix + "_" + mname
4330+ end
42764331 end
42774332 body = @nd_body[nid]
42784333 # Store module info for include
@@ -4282,6 +4337,19 @@
42824337 return
42834338 end
42844339 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+ end
4346+ }
4347+ body_stmts.each { |sid|
4348+ if @nd_type[sid] == "ClassNode"
4349+ collect_class_with_prefix(sid, mname)
4350+ end
4351+ }
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 >= 0
4676- 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 >= 0
46804748 init_ci = find_init_class(ci)
@@ -13177,15 +13245,15 @@
1317713245 end
1317813246
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] >= 0
1318413252 @needs_proc = 1
1318513253 return compile_proc_literal(nid)
1318613254 end
1318713255 end
13188- if cname == "Array"
13256+ if @nd_type[recv] == "ConstantReadNode" && cname == "Array"
1318913257 @needs_gc = 1
1319013258 args_id = @nd_arguments[nid]
1319113259 if args_id >= 0
@@ -13217,7 +13285,7 @@
1321713285 @needs_int_array = 1
1321813286 return "sp_IntArray_new()"
1321913287 end
13220- if cname == "Hash"
13288+ if @nd_type[recv] == "ConstantReadNode" && cname == "Hash"
1322113289 @needs_str_int_hash = 1
1322213290 @needs_gc = 1
1322313291 args_id = @nd_arguments[nid]
@@ -13237,7 +13305,7 @@
1323713305 end
1323813306 return "sp_StrIntHash_new()"
1323913307 end
13240- if cname == "StringIO"
13308+ if @nd_type[recv] == "ConstantReadNode" && cname == "StringIO"
1324113309 @needs_stringio = 1
1324213310 args_id = @nd_arguments[nid]
1324313311 if args_id >= 0