lang: array-typed '::' consts as immutable globals (PLAN-CONST-AGG step 1)

K : [4]s64 : .[...] and the untyped A :: .[1, 2, 3] register as
is_const globals: one storage, reads GEP it, dead-global elimination
drops unused ones, source-aware reads come free via selectGlobalAuthor.

- registerConstArrayGlobal (scanDecls pass 2): typed via the annotation
  (array-ness + dimension/count checked), untyped via element-type
  unification — all ints s64; ANY float promotes the element type to
  f64 with ints converting exactly; bool/string homogeneous; a
  non-numeric mix or non-inferable element asks for an annotation.
- constExprValue converts int elements into float destinations exactly
  (the int+float promotion rule, element-wise).
- emitGlobals marks is_const globals LLVMSetGlobalConstant — also flips
  the comptime-backed #run globals and __sx_default_context to
  'constant' (37 pinned IR snapshots regenerated; runtime unchanged).
- Element shapes: nested arrays, struct elements, strings, bools.
  Non-constant elements / dim mismatch / mixed types diagnose loudly.

Examples: 0177 (feature matrix incl. @K reads through *[4]s64 — needs
the 0117 fix), 1159/1160/1161 (diagnostics), 0837 repointed to values.
This commit is contained in:
agra
2026-06-11 12:26:26 +03:00
parent 82d6b8da0e
commit 8908e78943
61 changed files with 273 additions and 58 deletions

View File

@@ -50,7 +50,12 @@ pub fn constArrayLiteral(self: *Lowering, elements: []const *const Node, array_t
/// expression is not constant-foldable here.
pub fn constExprValue(self: *Lowering, expr: *const Node, expected_ty: TypeId) ?inst_mod.ConstantValue {
return switch (expr.data) {
.int_literal => |il| .{ .int = il.value },
// An int element in a FLOAT destination converts exactly (the
// int+float promotion rule, element-wise — `[2]f64 : .[1, 2.5]`).
.int_literal => |il| if (isFloat(expected_ty))
.{ .float = @floatFromInt(il.value) }
else
.{ .int = il.value },
.bool_literal => |bl| .{ .boolean = bl.value },
// A float into an INTEGER destination follows the implicit
// narrowing rule: an integral float folds to its int, a
@@ -71,7 +76,10 @@ pub fn constExprValue(self: *Lowering, expr: *const Node, expected_ty: TypeId) ?
.null_literal => .null_val,
.unary_op => |uo| switch (uo.op) {
.negate => switch (uo.operand.data) {
.int_literal => |il| .{ .int = -il.value },
.int_literal => |il| if (isFloat(expected_ty))
.{ .float = @floatFromInt(-il.value) }
else
.{ .int = -il.value },
.float_literal => |fl| .{ .float = -fl.value },
else => null,
},