Commit b54c50b0
b54c50b00120Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
Merge pull request #306 from ryanseys/rs-node-alias-method
feat(parse,codegen): support AliasMethodNode (alias greet hello)
Details
- Commit
- b54c50b00120
- Parent
- 002de75cb713
- Merge parent
- d5773c1d778c
spinel_codegen.rbmodifiedView file
@@ -6416,6 +6416,19 @@
64166416
end64176417
}64186418
6419+
# Third pass: handle alias / undef inside the class body. Must6420+
# run AFTER all own methods + included module methods are6421+
# registered, so the alias source can be located.6422+
body_stmts.each { |sid|6423+
if @nd_type[sid] == "AliasMethodNode"6424+
nn = symbol_node_literal(@nd_new_name[sid])6425+
on = symbol_node_literal(@nd_old_name[sid])6426+
if nn != "" && on != ""6427+
collect_class_method_alias(ci, nn, on)6428+
end6429+
end6430+
}6431+
64196432
# Collect ivars. Pin the lexical scope to this class so any64206433
# `@x = Foo.new(...)` inside its methods resolves `Foo` against64216434
# the same scope chain the call site sees — without this,@@ -6429,6 +6442,51 @@
64296442
@current_class_idx = saved_idx64306443
end64316444
6445+
# Extract the literal name from a SymbolNode argument. Returns ""6446+
# for InterpolatedSymbolNode and other shapes Spinel doesn't6447+
# support as alias source/target at compile time.6448+
def symbol_node_literal(nid)6449+
if nid < 06450+
return ""6451+
end6452+
if @nd_type[nid] == "SymbolNode"6453+
v = @nd_content[nid]6454+
if v == ""6455+
v = @nd_name[nid]6456+
end6457+
return v6458+
end6459+
""6460+
end6461+
6462+
# `alias new old` -- copy the existing class method's slot to a6463+
# new name in @cls_meth_*. CRuby snapshots the method body at6464+
# alias time; copying the body_id (a number, not a reference)6465+
# gives the same snapshot semantics: a later `def old` redefinition6466+
# would assign a new body_id to the old slot but the alias slot6467+
# keeps the original.6468+
def collect_class_method_alias(ci, new_name, old_name)6469+
src = cls_find_method_direct(ci, old_name)6470+
if src < 06471+
$stderr.puts "Spinel: alias `" + new_name + "` -> `" + old_name + "`: source method not found in class " + @cls_names[ci]6472+
exit(1)6473+
end6474+
pnames_all = @cls_meth_params[ci].split("|")6475+
ptypes_all = @cls_meth_ptypes[ci].split("|")6476+
rets_all = @cls_meth_returns[ci].split(";")6477+
bodies_all = @cls_meth_bodies[ci].split(";")6478+
defs_all = @cls_meth_defaults[ci].split("|")6479+
params = pnames_all[src] || ""6480+
ptypes = ptypes_all[src] || ""6481+
ret = rets_all[src] || "int"6482+
body_id = bodies_all[src].to_i6483+
defs = defs_all[src] || ""6484+
append_cls_meth(ci, new_name, params, ptypes, ret, body_id, defs)6485+
if @cls_meth_has_yield[ci] != ""6486+
@cls_meth_has_yield[ci] = @cls_meth_has_yield[ci] + ";0"6487+
end6488+
end6489+
64326490
def collect_module_methods_into_class(ci, mod_name)64336491
# Find the module and add its methods to the class64346492
mi = 0@@ -26051,6 +26109,13 @@
2605126109
# rewrite. Nothing to emit at runtime.2605226110
return2605326111
end26112+
if t == "AliasMethodNode"26113+
# `alias new old` -- compile-time only. The class-collect pass26114+
# already registered a duplicate method-table entry under the26115+
# new name pointing to the same body_id; subsequent dispatch26116+
# finds it like any other method.26117+
return26118+
end2605426119
if t == "LocalVariableWriteNode"2605526120
lname = @nd_name[nid]2605626121
# Check for method(:name) assignmentspinel_parse.cmodifiedView file
Loading diff…
test/alias_method.rbaddedView file
Loading diff…
test/alias_method.rb.expectedaddedView file
Loading diff…