Commit 26ed1c91
26ed1c916a4aMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
Merge pull request #307 from ryanseys/rs-up-undef
feat(parse,codegen): support UndefNode (undef foo, bar)
Details
- Commit
- 26ed1c916a4a
- Parent
- d7db4dd36138
- Merge parent
- 247167f6d129
spinel_codegen.rbmodifiedView file
@@ -102,6 +102,8 @@
102102
# for globals).103103
@nd_new_name = []104104
@nd_old_name = []105+
# UndefNode -- comma-separated child ids for the SymbolNode names.106+
@nd_names = "".split(",")105107
106108
@nd_count = 0107109
@root_id = 0@@ -215,6 +217,13 @@
215217
@galias_new = "".split(",")216218
@galias_old = "".split(",")217219
220+
# `undef foo` -- per-(class, method-name) registry of removed221+
# methods. Recorded by collect_class_method_undef; compile-time222+
# enforcement of "call after undef fails" is currently a223+
# documented out-of-scope.224+
@undef_class_idx = []225+
@undef_method = "".split(",")226+
218227
# ---- Scope stack for local variables ----219228
@scope_names = "".split(",")220229
@scope_types = "".split(",")@@ -506,6 +515,7 @@
506515
@nd_posts.push("")507516
@nd_new_name.push(-1)508517
@nd_old_name.push(-1)518+
@nd_names.push("")509519
@nd_count = @nd_count + 1510520
nid511521
end@@ -834,6 +844,10 @@
834844
if field == "posts"835845
@nd_posts[nid] = ids_str836846
end847+
if field == "names"848+
# UndefNode -- list of SymbolNode names to undef.849+
@nd_names[nid] = ids_str850+
end837851
end838852
839853
# ---- Convenience: get stmts of a body node ----@@ -6494,8 +6508,24 @@
64946508
collect_class_method_alias(ci, nn, on)64956509
end64966510
end6511+
if @nd_type[sid] == "UndefNode"6512+
# `undef foo, bar` -- record the removal in @undef_*. Spinel6513+
# doesn't currently enforce "calling an undef'd method6514+
# fails" at the compile-time dispatch path; the recording6515+
# is the foundation for that future check.6516+
unames = parse_id_list(@nd_names[sid])6517+
uk = 06518+
while uk < unames.length6519+
uname = symbol_node_literal(unames[uk])6520+
if uname != ""6521+
collect_class_method_undef(ci, uname)6522+
end6523+
uk = uk + 16524+
end6525+
end64976526
}64986527
6528+
64996529
# Collect ivars. Pin the lexical scope to this class so any65006530
# `@x = Foo.new(...)` inside its methods resolves `Foo` against65016531
# the same scope chain the call site sees — without this,@@ -6511,7 +6541,7 @@
65116541
65126542
# Extract the literal name from a SymbolNode argument. Returns ""65136543
# for InterpolatedSymbolNode and other shapes Spinel doesn't6514-
# support as alias source/target at compile time.6544+
# support as alias source/target or undef target at compile time.65156545
def symbol_node_literal(nid)65166546
if nid < 065176547
return ""@@ -6554,6 +6584,16 @@
65546584
end65556585
end65566586
6587+
# `undef foo` -- mark the method as removed in the @undef_*6588+
# tracker. Spinel currently doesn't enforce "call after undef6589+
# fails to compile" -- the recording is the foundation for that6590+
# future check. Documented out of scope in test/undef.rb.6591+
def collect_class_method_undef(ci, name)6592+
@undef_class_idx.push(ci)6593+
@undef_method.push(name)6594+
end6595+
6596+
65576597
def collect_module_methods_into_class(ci, mod_name)65586598
# Find the module and add its methods to the class65596599
mi = 0@@ -26232,6 +26272,11 @@
2623226272
# finds it like any other method.2623326273
return2623426274
end26275+
if t == "UndefNode"26276+
# `undef foo` -- compile-time only. The class-collect pass26277+
# already recorded the removal in @undef_*; nothing to emit.26278+
return26279+
end2623526280
if t == "LocalVariableWriteNode"2623626281
lname = @nd_name[nid]2623726282
# Check for method(:name) assignmentspinel_parse.cmodifiedView file
Loading diff…
test/undef.rbaddedView file
Loading diff…
test/undef.rb.expectedaddedView file
Loading diff…