Commit 5f3707d3
5f3707d3fb46Merged
sacckey <[email protected]> · 6 months ago · 2 files · ~2 modified
Description
fix(codegen): resolve ConstantPath with lexical/root scope and unify scoped constant collection
Details
- Commit
- 5f3707d3fb46
- Parent
- 3e7baed8f413
spinel_codegen.rbmodifiedView file
@@ -743,7 +743,109 @@
743743
end744744
rt = @nd_type[recv_nid]745745
if rt == "ConstantReadNode" || rt == "ConstantPathNode"746-
return const_ref_flat_name(recv_nid)746+
return resolve_const_ref_name(recv_nid)747+
end748+
""749+
end750+
751+
def module_name_exists(name)752+
i = 0753+
while i < @module_names.length754+
if @module_names[i] == name755+
return 1756+
end757+
i = i + 1758+
end759+
0760+
end761+
762+
def builtin_const_root_name(name)763+
if name == "Float" || name == "Integer" || name == "Math"764+
return 1765+
end766+
if name == "Array" || name == "Hash" || name == "Proc" || name == "StringIO" || name == "Fiber"767+
return 1768+
end769+
0770+
end771+
772+
def const_namespace_exists(name)773+
if name == ""774+
return 0775+
end776+
if find_const_idx(name) >= 0777+
return 1778+
end779+
if find_class_idx(name) >= 0780+
return 1781+
end782+
if module_name_exists(name) == 1783+
return 1784+
end785+
if builtin_const_root_name(name) == 1786+
return 1787+
end788+
0789+
end790+
791+
def current_lexical_scope_name792+
if @current_class_idx >= 0793+
if @current_class_idx < @cls_names.length794+
return @cls_names[@current_class_idx]795+
end796+
return ""797+
end798+
if @current_method_name != ""799+
cls_idx = @current_method_name.index("_cls_")800+
if cls_idx >= 0801+
return @current_method_name[0, cls_idx]802+
end803+
end804+
""805+
end806+
807+
def trim_const_scope_once(name)808+
if name == ""809+
return ""810+
end811+
idx = name.rindex("_")812+
if idx < 0813+
return ""814+
end815+
name[0, idx]816+
end817+
818+
def resolve_const_read_name(name)819+
scope = current_lexical_scope_name820+
while scope != ""821+
cand = scope + "_" + name822+
if const_namespace_exists(cand) == 1823+
return cand824+
end825+
scope = trim_const_scope_once(scope)826+
end827+
name828+
end829+
830+
def resolve_const_ref_name(nid)831+
if nid < 0832+
return ""833+
end834+
t = @nd_type[nid]835+
if t == "ConstantReadNode"836+
return resolve_const_read_name(@nd_name[nid])837+
end838+
if t == "ConstantPathNode"839+
leaf = @nd_name[nid]840+
parent = @nd_receiver[nid]841+
if parent < 0842+
return leaf843+
end844+
base = resolve_const_ref_name(parent)845+
if base == ""846+
return ""847+
end848+
return base + "_" + leaf747849
end748850
""749851
end@@ -1045,48 +1147,28 @@
10451147
if @nd_name[nid] == "ARGV"10461148
return "argv"10471149
end1048-
if @current_class_idx >= 01049-
cpname = @cls_names[@current_class_idx] + "_" + @nd_name[nid]1050-
ci2 = find_const_idx(cpname)1051-
if ci2 >= 01052-
return @const_types[ci2]1053-
end1054-
end1055-
ci = find_const_idx(@nd_name[nid])1150+
rname = resolve_const_read_name(@nd_name[nid])1151+
ci = find_const_idx(rname)10561152
if ci >= 010571153
return @const_types[ci]10581154
end1059-
cx = find_class_idx(@nd_name[nid])1155+
cx = find_class_idx(rname)10601156
if cx >= 01061-
return "class_" + @nd_name[nid]1062-
end1063-
# Check module-prefixed constants1064-
mi3 = 01065-
while mi3 < @module_names.length1066-
mmod = @module_names[mi3]1067-
if mmod != ""1068-
cpname = mmod + "_" + @nd_name[nid]1069-
ci4 = find_const_idx(cpname)1070-
if ci4 >= 01071-
return @const_types[ci4]1072-
end1073-
end1074-
mi3 = mi3 + 11157+
return "class_" + rname10751158
end10761159
return "int"10771160
end10781161
if t == "ConstantPathNode"1079-
if @nd_receiver[nid] >= 01080-
rname = const_ref_flat_name(@nd_receiver[nid])1081-
nname = @nd_name[nid]1082-
if rname == ""1083-
return "int"1084-
end1085-
cpname = rname + "_" + nname1162+
cpname = resolve_const_ref_name(nid)1163+
if cpname != ""10861164
ci = find_const_idx(cpname)10871165
if ci >= 010881166
return @const_types[ci]10891167
end1168+
end1169+
parent = @nd_receiver[nid]1170+
if parent >= 01171+
rname = resolve_const_ref_name(parent)10901172
if rname == "Float"10911173
return "float"10921174
end@@ -3473,8 +3555,26 @@
34733555
end34743556
34753557
def collect_scoped_constant(scope_name, nid)3476-
cname = scope_name + "_" + @nd_name[nid]3558+
cname = @nd_name[nid]3559+
if scope_name != ""3560+
cname = scope_name + "_" + cname3561+
end34773562
expr_id = @nd_expression[nid]3563+
if expr_id >= 03564+
if @nd_type[expr_id] == "CallNode"3565+
if @nd_name[expr_id] == "new"3566+
sr = @nd_receiver[expr_id]3567+
if sr >= 03568+
if @nd_type[sr] == "ConstantReadNode"3569+
if @nd_name[sr] == "Struct"3570+
collect_struct_class(cname, expr_id)3571+
return3572+
end3573+
end3574+
end3575+
end3576+
end3577+
end34783578
ct = "int"34793579
if expr_id >= 034803580
ct = infer_type(expr_id)@@ -4468,15 +4568,7 @@
44684568
44694569
body_stmts.each { |sid|44704570
if @nd_type[sid] == "ConstantWriteNode"4471-
cname = mname + "_" + @nd_name[sid]4472-
expr_id = @nd_expression[sid]4473-
ct = "int"4474-
if expr_id >= 04475-
ct = infer_type(expr_id)4476-
end4477-
@const_names.push(cname)4478-
@const_types.push(ct)4479-
@const_expr_ids.push(expr_id)4571+
collect_scoped_constant(mname, sid)44804572
end44814573
# Collect module class methods (def self.xxx) as top-level functions44824574
if @nd_type[sid] == "DefNode"@@ -4511,30 +4603,7 @@
45114603
end45124604
45134605
def collect_constant(nid)4514-
# Check for Struct.new(:x, :y)4515-
expr_id = @nd_expression[nid]4516-
if expr_id >= 04517-
if @nd_type[expr_id] == "CallNode"4518-
if @nd_name[expr_id] == "new"4519-
sr = @nd_receiver[expr_id]4520-
if sr >= 04521-
if @nd_type[sr] == "ConstantReadNode"4522-
if @nd_name[sr] == "Struct"4523-
collect_struct_class(@nd_name[nid], expr_id)4524-
return4525-
end4526-
end4527-
end4528-
end4529-
end4530-
end4531-
@const_names.push(@nd_name[nid])4532-
ct = "int"4533-
if expr_id >= 04534-
ct = infer_type(expr_id)4535-
end4536-
@const_types.push(ct)4537-
@const_expr_ids.push(expr_id)4606+
collect_scoped_constant("", nid)45384607
end45394608
45404609
def collect_struct_class(cname, call_nid)@@ -11363,61 +11432,29 @@
1136311432
if @nd_name[nid] == "ARGV"1136411433
return "sp_argv"1136511434
end11366-
if @current_class_idx >= 011367-
cpname_cls = @cls_names[@current_class_idx] + "_" + @nd_name[nid]11368-
ci_cls = find_const_idx(cpname_cls)11369-
if ci_cls >= 011370-
lv_cls = const_literal_c_value(ci_cls)11371-
if lv_cls != ""11372-
return lv_cls11373-
end11374-
return "cst_" + cpname_cls11375-
end11376-
end11377-
ci = find_const_idx(@nd_name[nid])11435+
rname = resolve_const_read_name(@nd_name[nid])11436+
ci = find_const_idx(rname)1137811437
if ci >= 01137911438
# Propagate simple literal constants to their use sites.1138011439
lv = const_literal_c_value(ci)1138111440
if lv != ""1138211441
return lv1138311442
end11384-
return "cst_" + @nd_name[nid]11385-
end11386-
# Check if inside a module method and constant belongs to that module11387-
mi3 = 011388-
while mi3 < @module_names.length11389-
mmod = @module_names[mi3]11390-
if mmod != ""11391-
if @current_method_name.start_with?(mmod + "_cls_")11392-
cpname = mmod + "_" + @nd_name[nid]11393-
ci4 = find_const_idx(cpname)11394-
if ci4 >= 011395-
return "cst_" + cpname11396-
end11397-
end11398-
# Also check when in main scope (module constants referenced at top level)11399-
cpname = mmod + "_" + @nd_name[nid]11400-
ci5 = find_const_idx(cpname)11401-
if ci5 >= 011402-
return "cst_" + cpname11403-
end11404-
end11405-
mi3 = mi3 + 111443+
return "cst_" + rname1140611444
end11407-
return @nd_name[nid]11445+
return rname1140811446
end1140911447
if t == "ConstantPathNode"11410-
if @nd_receiver[nid] >= 011411-
rname = const_ref_flat_name(@nd_receiver[nid])11412-
nname = @nd_name[nid]11413-
if rname == ""11414-
return @nd_name[nid]11415-
end11416-
cpname = rname + "_" + nname11448+
cpname = resolve_const_ref_name(nid)11449+
if cpname != ""1141711450
ci = find_const_idx(cpname)1141811451
if ci >= 01141911452
return "cst_" + cpname1142011453
end11454+
end11455+
if @nd_receiver[nid] >= 011456+
rname = resolve_const_ref_name(@nd_receiver[nid])11457+
nname = @nd_name[nid]1142111458
# Built-in constants1142211459
if rname == "Float"1142311460
if nname == "INFINITY"@@ -11440,7 +11477,9 @@
1144011477
return "2.71828182845904523536"1144111478
end1144211479
end11443-
return cpname11480+
if cpname != ""11481+
return cpname11482+
end1144411483
end1144511484
return @nd_name[nid]1144611485
endtest/constant_path_nested_read.rbmodifiedView file
Loading diff…