Commit 3f8973a7
3f8973a7e8a5Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
Merge pull request #298 from ryanseys/rs-node-alias-global-var
feat(parse,codegen): support AliasGlobalVariableNode (alias $copy $orig)
Details
- Commit
- 3f8973a7e8a5
- Parent
- f5fc728473ba
- Merge parent
- b7c6eaa4e0f5
spinel_codegen.rbmodifiedView file
@@ -97,6 +97,11 @@
9797
# (post-rest parameters aren't observed in test/), but the parser9898
# emits the field so future tests get a proper AST.9999
@nd_posts = "".split(",")100+
# AliasMethodNode / AliasGlobalVariableNode -- parallel ref slots101+
# for the new and old names (SymbolNode for methods, GlobalVariableReadNode102+
# for globals).103+
@nd_new_name = []104+
@nd_old_name = []100105
101106
@nd_count = 0102107
@root_id = 0@@ -203,6 +208,13 @@
203208
@redo_label_stack = "".split(",")204209
@redo_label_counter = 0205210
211+
# `alias $copy $orig` -- maps new gvar name to its target.212+
# Populated by collect_all from AliasGlobalVariableNode213+
# statements; consulted by sanitize_gvar / scan_features /214+
# infer_type so $copy and $orig share storage.215+
@galias_new = "".split(",")216+
@galias_old = "".split(",")217+
206218
# ---- Scope stack for local variables ----207219
@scope_names = "".split(",")208220
@scope_types = "".split(",")@@ -485,6 +497,8 @@
485497
@nd_targets.push("")486498
@nd_rights.push("")487499
@nd_posts.push("")500+
@nd_new_name.push(-1)501+
@nd_old_name.push(-1)488502
@nd_count = @nd_count + 1489503
nid490504
end@@ -753,6 +767,14 @@
753767
if field == "call"754768
@nd_receiver[nid] = ref_id755769
end770+
if field == "new_name"771+
# AliasMethodNode / AliasGlobalVariableNode -- the new-name slot772+
# (a SymbolNode for methods, GlobalVariableReadNode for globals).773+
@nd_new_name[nid] = ref_id774+
end775+
if field == "old_name"776+
@nd_old_name[nid] = ref_id777+
end756778
end757779
758780
def set_array_field(nid, field, ids_str)@@ -1912,7 +1934,10 @@
19121934
return "int"19131935
end19141936
if t == "GlobalVariableReadNode"1915-
gname = @nd_name[nid]1937+
# `alias $copy $orig` -- a $copy read must look up $orig's1938+
# registered type so the C codegen sees the correct format1939+
# specifier when interpolating or printing.1940+
gname = resolve_gvar_alias(@nd_name[nid])19161941
gi = 019171942
while gi < @gvar_names.length19181943
if @gvar_names[gi] == gname@@ -5010,6 +5035,12 @@
50105035
end50115036
50125037
def sanitize_gvar(name)5038+
# `alias $copy $orig` -- transparently rewrite `$copy` to `$orig`5039+
# everywhere it appears so the C output uses one storage slot5040+
# for both names. Compile-time only; matches CRuby's5041+
# `$copy and $orig share storage` semantics for the static-alias5042+
# case.5043+
name = resolve_gvar_alias(name)50135044
# $last → gv_last, $1 → gv_150145045
if name.length > 0 && name[0] == "$"50155046
return "gv_" + name[1, name.length - 1]@@ -5017,6 +5048,17 @@
50175048
"gv_" + name50185049
end50195050
5051+
def resolve_gvar_alias(name)5052+
i = 05053+
while i < @galias_new.length5054+
if @galias_new[i] == name5055+
return @galias_old[i]5056+
end5057+
i = i + 15058+
end5059+
name5060+
end5061+
50205062
# ---- Array type helpers ----50215063
def array_c_prefix(t)50225064
if t == "str_array"@@ -5440,6 +5482,20 @@
54405482
end54415483
}54425484
5485+
# Top-level `alias $copy $orig`. Records the rewrite in5486+
# @galias_* so sanitize_gvar / scan_features / infer_type5487+
# treat $copy and $orig as the same storage slot.5488+
stmts.each { |sid|5489+
if @nd_type[sid] == "AliasGlobalVariableNode"5490+
nn = @nd_name[@nd_new_name[sid]]5491+
on = @nd_name[@nd_old_name[sid]]5492+
if nn != "" && on != ""5493+
@galias_new.push(nn)5494+
@galias_old.push(on)5495+
end5496+
end5497+
}5498+
54435499
# Pass 2.6: hoist `recv.instance_eval do ... end` blocks into54445500
# file-scope static functions. Receiver-class flow analysis picks the54455501
# receiver's class, the block body is later compiled as a function@@ -11693,7 +11749,12 @@
1169311749
end1169411750
1169511751
if t == "GlobalVariableWriteNode"11696-
gname = @nd_name[nid]11752+
# `alias $copy $orig` -- a $copy = ... write must register11753+
# the type under $orig's slot, not a separate $copy slot,11754+
# otherwise the type-consistency check below would later see11755+
# $orig and $copy as two slots that resolve to the same C11756+
# global and fire a spurious type-mismatch error.11757+
gname = resolve_gvar_alias(@nd_name[nid])1169711758
if gname != "$stderr" && gname != "$stdout" && gname != "$?"1169811759
gt = infer_type(@nd_expression[nid])1169911760
if not_in(gname, @gvar_names) == 1@@ -11715,7 +11776,9 @@
1171511776
end1171611777
end1171711778
if t == "GlobalVariableReadNode"11718-
gname = @nd_name[nid]11779+
# Same alias resolution as the write side -- $copy reads must11780+
# land on $orig's slot.11781+
gname = resolve_gvar_alias(@nd_name[nid])1171911782
if gname != "$stderr" && gname != "$stdout" && gname != "$?"1172011783
if not_in(gname, @gvar_names) == 11172111784
@gvar_names.push(gname)@@ -25398,6 +25461,13 @@
2539825461
emit(" goto " + lbl + ";")2539925462
return2540025463
end25464+
if t == "AliasGlobalVariableNode"25465+
# `alias $copy $orig` -- compile-time only. collect_all25466+
# already populated @galias_*; references through25467+
# sanitize_gvar / scan_features / infer_type pick up the25468+
# rewrite. Nothing to emit at runtime.25469+
return25470+
end2540125471
if t == "LocalVariableWriteNode"2540225472
lname = @nd_name[nid]2540325473
# Check for method(:name) assignmentspinel_parse.cmodifiedView file
Loading diff…
test/alias_global.rbaddedView file
Loading diff…
test/alias_global.rb.expectedaddedView file
Loading diff…