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 end
64176417 }
64186418
6419+ # Third pass: handle alias / undef inside the class body. Must
6420+ # run AFTER all own methods + included module methods are
6421+ # 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+ end
6429+ end
6430+ }
6431+
64196432 # Collect ivars. Pin the lexical scope to this class so any
64206433 # `@x = Foo.new(...)` inside its methods resolves `Foo` against
64216434 # the same scope chain the call site sees — without this,
@@ -6429,6 +6442,51 @@
64296442 @current_class_idx = saved_idx
64306443 end
64316444
6445+ # Extract the literal name from a SymbolNode argument. Returns ""
6446+ # for InterpolatedSymbolNode and other shapes Spinel doesn't
6447+ # support as alias source/target at compile time.
6448+ def symbol_node_literal(nid)
6449+ if nid < 0
6450+ return ""
6451+ end
6452+ if @nd_type[nid] == "SymbolNode"
6453+ v = @nd_content[nid]
6454+ if v == ""
6455+ v = @nd_name[nid]
6456+ end
6457+ return v
6458+ end
6459+ ""
6460+ end
6461+
6462+ # `alias new old` -- copy the existing class method's slot to a
6463+ # new name in @cls_meth_*. CRuby snapshots the method body at
6464+ # alias time; copying the body_id (a number, not a reference)
6465+ # gives the same snapshot semantics: a later `def old` redefinition
6466+ # would assign a new body_id to the old slot but the alias slot
6467+ # 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 < 0
6471+ $stderr.puts "Spinel: alias `" + new_name + "` -> `" + old_name + "`: source method not found in class " + @cls_names[ci]
6472+ exit(1)
6473+ end
6474+ 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_i
6483+ 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+ end
6488+ end
6489+
64326490 def collect_module_methods_into_class(ci, mod_name)
64336491 # Find the module and add its methods to the class
64346492 mi = 0
@@ -26051,6 +26109,13 @@
2605126109 # rewrite. Nothing to emit at runtime.
2605226110 return
2605326111 end
26112+ if t == "AliasMethodNode"
26113+ # `alias new old` -- compile-time only. The class-collect pass
26114+ # already registered a duplicate method-table entry under the
26115+ # new name pointing to the same body_id; subsequent dispatch
26116+ # finds it like any other method.
26117+ return
26118+ end
2605426119 if t == "LocalVariableWriteNode"
2605526120 lname = @nd_name[nid]
2605626121 # Check for method(:name) assignment