Files
sx/examples/0177-types-array-consts.sx
agra 8908e78943 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.
2026-06-11 12:26:26 +03:00

35 lines
1.3 KiB
Plaintext

// Array-typed `::` constants are IMMUTABLE GLOBALS: one storage, reads
// GEP it, the emitter marks it constant, dead-global elimination drops
// unused ones. Typed (`K : [4]s64 : .[...]`) and untyped (`A :: .[...]`,
// element type inferred — all ints s64; any float promotes the element
// type to f64 with ints converting exactly; bool/string homogeneous).
// Element shapes cover nested aggregates. Reads are normal array values:
// indexing, .len, by-value copies (independent of the const), passing to
// fixed-array params, and @-address (reads through *[4]s64 — issue 0117).
#import "modules/std.sx";
K : [4]s64 : .[11, 22, 33, 44];
A :: .[1, 2, 3];
M :: .[1, 2.2, 3];
F : [2]f64 : .[1, 2.5];
S :: .["alpha", "beta"];
B :: .[true, false, true];
P :: struct { x, y: s64; }
PTS : [2]P : .[P.{ x = 1, y = 2 }, P.{ x = 3, y = 4 }];
GRID : [2][2]s64 : .[.[1, 2], .[3, 4]];
sum :: (xs: [4]s64) -> s64 { xs[0] + xs[1] + xs[2] + xs[3] }
main :: () {
print("typed={} untyped={} len={}\n", K[2], A[1], K.len);
print("mixed={} {} {} intfloat={}\n", M[0], M[1], M[2], F[0]);
print("str={} bool={} pt={} grid={}\n", S[1], B[2], PTS[1].y, GRID[1][0]);
k := K;
k[0] = 99;
print("copy={} const={}\n", k[0], K[0]);
print("sum={}\n", sum(K));
p := @K;
print("via-ptr={}\n", p[2]);
}