Commit 1b82c20f
1b82c20f5c96Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
Merge pull request #277 from ryanseys/rs-node-class-var-write
feat(parse,codegen): support ClassVariableWriteNode (@@var = val)
Details
- Commit
- 1b82c20f5c96
- Parent
- 1755124b4204
- Merge parent
- 8cb219b64317
spinel_codegen.rbmodifiedView file
@@ -165,6 +165,17 @@
165165
# ---- Constants (parallel arrays) ----166166
@const_names = "".split(",")167167
@const_types = "".split(",")168+
169+
# ---- Class variables (@@var) ----170+
# Per-(class,name) parallel arrays. Storage is a per-class C global171+
# named `cvar_<ClassName>_<var>` (var without the @@ prefix).172+
# Spinel's class-var lookup does NOT walk the inheritance chain --173+
# each class's @@var is independent. CRuby's hierarchy-shared cvars174+
# are a known footgun (mame, ko1, et al. publicly disrecommend175+
# them); the simpler per-class semantics fit Spinel's compile-time176+
# storage model better. Documented in the test fixtures.177+
@cvar_names = "".split(",")178+
@cvar_types = "".split(",")168179
@const_expr_ids = []169180
@const_scope_names = "".split(",")170181
@@ -1525,6 +1536,51 @@
15251536
-115261537
end15271538
1539+
# ---- Class variable helpers ----1540+
# Qualified name for a class-var slot: `<ClassName>_<var>` where1541+
# <var> drops the leading @@. Codegen emits one C global named1542+
# `cvar_<qname>` per registered cvar.1543+
def cvar_qname(class_idx, var_name)1544+
cls = "Toplevel"1545+
if class_idx >= 01546+
cls = @cls_names[class_idx]1547+
end1548+
bare = var_name1549+
if bare.length >= 2 && bare[0, 2] == "@@"1550+
bare = bare[2, bare.length - 2]1551+
end1552+
cls + "_" + bare1553+
end1554+
1555+
def find_cvar_idx(qname)1556+
i = 01557+
while i < @cvar_names.length1558+
if @cvar_names[i] == qname1559+
return i1560+
end1561+
i = i + 11562+
end1563+
-11564+
end1565+
1566+
# Register or update a cvar's inferred type. Called from1567+
# scan_class_vars during the pre-pass and (defensively) again from1568+
# compile_stmt when the write fires.1569+
def register_cvar(qname, t)1570+
ci = find_cvar_idx(qname)1571+
if ci >= 01572+
# Widen on type disagreement: int + string -> poly. Conservative1573+
# for v1 -- when the gap matters we'll revisit.1574+
if @cvar_types[ci] != t && @cvar_types[ci] != "poly" && t != ""1575+
@cvar_types[ci] = "poly"1576+
end1577+
return ci1578+
end1579+
@cvar_names.push(qname)1580+
@cvar_types.push(t)1581+
@cvar_names.length - 11582+
end1583+
15281584
# If the constant's initializer is a simple literal, return the15291585
# corresponding C expression. Otherwise return "" so callers fall15301586
# back to cst_<name> lookup. Enables propagation of:@@ -5239,6 +5295,11 @@
52395295
collect_class(sid)52405296
end52415297
}5298+
# Pass 1.4: register class variables (@@var). Walks each class5299+
# body for any ClassVariable*WriteNode (Write, Operator, Or, And,5300+
# Target) and records the inferred type per (class, name). The5301+
# static C globals are emitted in pass-emit alongside constants.5302+
collect_cvars52425303
# Pass 1.5: reject circular inheritance (`class A < B; class B < A`).52435304
# Every parent-walking helper (cls_find_method, cls_ivar_type,52445305
# is_class_or_ancestor, …) recurses through @cls_parents; a cycle@@ -5703,6 +5764,56 @@
57035764
collect_class_with_prefix(nid, "")57045765
end57055766
5767+
# Walk every class body for class-var writes; register each5768+
# (class, name) pair so the static-declaration pass can emit5769+
# `static <type> cvar_<qname> = <default>;` ahead of the5770+
# functions that touch it.5771+
def collect_cvars5772+
root = @root_id5773+
if @nd_type[root] != "ProgramNode"5774+
return5775+
end5776+
stmts = get_body_stmts(root)5777+
stmts.each { |sid|5778+
if @nd_type[sid] == "ClassNode"5779+
cp = @nd_constant_path[sid]5780+
if cp >= 05781+
cname = const_ref_flat_name(cp)5782+
ci = find_class_idx(cname)5783+
if ci >= 05784+
body_id = @nd_body[sid]5785+
collect_cvars_in(body_id, ci)5786+
end5787+
end5788+
end5789+
}5790+
end5791+
5792+
# Recursively scan `nid`'s subtree for ClassVariable*WriteNode and5793+
# register each. Only the WriteNode form contributes a static type5794+
# at this stage; the compound forms (Operator/Or/And) are5795+
# registered when their parent ClassVariableWriteNode is seen, OR5796+
# lazily during compile_stmt if no plain Write precedes them in5797+
# the same class.5798+
def collect_cvars_in(nid, class_idx)5799+
if nid < 05800+
return5801+
end5802+
t = @nd_type[nid]5803+
if t == "ClassVariableWriteNode"5804+
qname = cvar_qname(class_idx, @nd_name[nid])5805+
val_t = infer_type(@nd_expression[nid])5806+
register_cvar(qname, val_t)5807+
end5808+
cs = []5809+
push_child_ids(nid, cs)5810+
k = 05811+
while k < cs.length5812+
collect_cvars_in(cs[k], class_idx)5813+
k = k + 15814+
end5815+
end5816+
57065817
def collect_scoped_constant(scope_name, nid)57075818
cname = @nd_name[nid]57085819
if scope_name != ""@@ -13282,6 +13393,18 @@
1328213393
if @const_names.length > 01328313394
emit_raw("")1328413395
end13396+
# Emit file-scope class-variable declarations. One C global per13397+
# (class, name) pair, named `cvar_<ClassName>_<var>`; populated13398+
# by the ClassVariable*WriteNode codegen arms.13399+
i = 013400+
while i < @cvar_names.length13401+
ctp = c_type(@cvar_types[i])13402+
emit_raw("static " + ctp + " cvar_" + @cvar_names[i] + " = " + c_default_val(@cvar_types[i]) + ";")13403+
i = i + 113404+
end13405+
if @cvar_names.length > 013406+
emit_raw("")13407+
end1328513408
# Issue #126 Stage 2: storage for module-level singleton accessors1328613409
# whose resolved set has 2+ candidates. The slot stores the1328713410
# assigned module's sentinel (mrb_int from `module_sentinel`); the@@ -24758,6 +24881,19 @@
2475824881
emit(" }")2475924882
return2476024883
end24884+
if t == "ClassVariableWriteNode"24885+
# `@@x = val` inside class body or method. Storage is the24886+
# per-(class,name) C global cvar_<ClassName>_<var> registered24887+
# by collect_cvars during the pre-pass. If the cvar wasn't seen24888+
# there (e.g. inside a class-method that pre-pass missed) we24889+
# register defensively.24890+
qname = cvar_qname(@current_class_idx, @nd_name[nid])24891+
val = compile_expr(@nd_expression[nid])24892+
val_t = infer_type(@nd_expression[nid])24893+
register_cvar(qname, val_t)24894+
emit(" cvar_" + qname + " = " + val + ";")24895+
return24896+
end2476124897
if t == "LocalVariableWriteNode"2476224898
lname = @nd_name[nid]2476324899
# Check for method(:name) assignmentspinel_parse.cmodifiedView file
Loading diff…
test/class_var_write.rbaddedView file
Loading diff…
test/class_var_write.rb.expectedaddedView file
Loading diff…