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 global
171+ # 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 cvars
174+ # are a known footgun (mame, ko1, et al. publicly disrecommend
175+ # them); the simpler per-class semantics fit Spinel's compile-time
176+ # 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 -1
15261537 end
15271538
1539+ # ---- Class variable helpers ----
1540+ # Qualified name for a class-var slot: `<ClassName>_<var>` where
1541+ # <var> drops the leading @@. Codegen emits one C global named
1542+ # `cvar_<qname>` per registered cvar.
1543+ def cvar_qname(class_idx, var_name)
1544+ cls = "Toplevel"
1545+ if class_idx >= 0
1546+ cls = @cls_names[class_idx]
1547+ end
1548+ bare = var_name
1549+ if bare.length >= 2 && bare[0, 2] == "@@"
1550+ bare = bare[2, bare.length - 2]
1551+ end
1552+ cls + "_" + bare
1553+ end
1554+
1555+ def find_cvar_idx(qname)
1556+ i = 0
1557+ while i < @cvar_names.length
1558+ if @cvar_names[i] == qname
1559+ return i
1560+ end
1561+ i = i + 1
1562+ end
1563+ -1
1564+ end
1565+
1566+ # Register or update a cvar's inferred type. Called from
1567+ # scan_class_vars during the pre-pass and (defensively) again from
1568+ # compile_stmt when the write fires.
1569+ def register_cvar(qname, t)
1570+ ci = find_cvar_idx(qname)
1571+ if ci >= 0
1572+ # Widen on type disagreement: int + string -> poly. Conservative
1573+ # 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+ end
1577+ return ci
1578+ end
1579+ @cvar_names.push(qname)
1580+ @cvar_types.push(t)
1581+ @cvar_names.length - 1
1582+ end
1583+
15281584 # If the constant's initializer is a simple literal, return the
15291585 # corresponding C expression. Otherwise return "" so callers fall
15301586 # back to cst_<name> lookup. Enables propagation of:
@@ -5239,6 +5295,11 @@
52395295 collect_class(sid)
52405296 end
52415297 }
5298+ # Pass 1.4: register class variables (@@var). Walks each class
5299+ # body for any ClassVariable*WriteNode (Write, Operator, Or, And,
5300+ # Target) and records the inferred type per (class, name). The
5301+ # static C globals are emitted in pass-emit alongside constants.
5302+ collect_cvars
52425303 # 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 end
57055766
5767+ # Walk every class body for class-var writes; register each
5768+ # (class, name) pair so the static-declaration pass can emit
5769+ # `static <type> cvar_<qname> = <default>;` ahead of the
5770+ # functions that touch it.
5771+ def collect_cvars
5772+ root = @root_id
5773+ if @nd_type[root] != "ProgramNode"
5774+ return
5775+ end
5776+ stmts = get_body_stmts(root)
5777+ stmts.each { |sid|
5778+ if @nd_type[sid] == "ClassNode"
5779+ cp = @nd_constant_path[sid]
5780+ if cp >= 0
5781+ cname = const_ref_flat_name(cp)
5782+ ci = find_class_idx(cname)
5783+ if ci >= 0
5784+ body_id = @nd_body[sid]
5785+ collect_cvars_in(body_id, ci)
5786+ end
5787+ end
5788+ end
5789+ }
5790+ end
5791+
5792+ # Recursively scan `nid`'s subtree for ClassVariable*WriteNode and
5793+ # register each. Only the WriteNode form contributes a static type
5794+ # at this stage; the compound forms (Operator/Or/And) are
5795+ # registered when their parent ClassVariableWriteNode is seen, OR
5796+ # lazily during compile_stmt if no plain Write precedes them in
5797+ # the same class.
5798+ def collect_cvars_in(nid, class_idx)
5799+ if nid < 0
5800+ return
5801+ end
5802+ 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+ end
5808+ cs = []
5809+ push_child_ids(nid, cs)
5810+ k = 0
5811+ while k < cs.length
5812+ collect_cvars_in(cs[k], class_idx)
5813+ k = k + 1
5814+ end
5815+ end
5816+
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 > 0
1328313394 emit_raw("")
1328413395 end
13396+ # Emit file-scope class-variable declarations. One C global per
13397+ # (class, name) pair, named `cvar_<ClassName>_<var>`; populated
13398+ # by the ClassVariable*WriteNode codegen arms.
13399+ i = 0
13400+ while i < @cvar_names.length
13401+ ctp = c_type(@cvar_types[i])
13402+ emit_raw("static " + ctp + " cvar_" + @cvar_names[i] + " = " + c_default_val(@cvar_types[i]) + ";")
13403+ i = i + 1
13404+ end
13405+ if @cvar_names.length > 0
13406+ emit_raw("")
13407+ end
1328513408 # Issue #126 Stage 2: storage for module-level singleton accessors
1328613409 # whose resolved set has 2+ candidates. The slot stores the
1328713410 # assigned module's sentinel (mrb_int from `module_sentinel`); the
@@ -24758,6 +24881,19 @@
2475824881 emit(" }")
2475924882 return
2476024883 end
24884+ if t == "ClassVariableWriteNode"
24885+ # `@@x = val` inside class body or method. Storage is the
24886+ # per-(class,name) C global cvar_<ClassName>_<var> registered
24887+ # by collect_cvars during the pre-pass. If the cvar wasn't seen
24888+ # there (e.g. inside a class-method that pre-pass missed) we
24889+ # 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+ return
24896+ end
2476124897 if t == "LocalVariableWriteNode"
2476224898 lname = @nd_name[nid]
2476324899 # Check for method(:name) assignment