lang: introduce cstring — the C-boundary string (Odin model)

cstring is ONE pointer to a null-terminated u8 buffer, C's char*: thin
(8 bytes, no length; cstring_len walks to the terminator), crossing
#foreign boundaries verbatim in both directions, with ?cstring as the
nullable case lowering to the same bare pointer (null = absent).

Conversion discipline mirrors Odin: a string LITERAL coerces implicitly
(its bytes are terminated constants); any other string is rejected with
a diagnostic naming to_cstring (it may be an unterminated view); and
cstring never coerces to string implicitly — from_cstring(c) is the
explicit zero-copy view, pricing the strlen.

Plumbing: TypeId/TypeInfo builtin slot 18 (first_user 19), name
classifiers, size/align/name tables, LLVM ptr lowering, the ?T pointer
niche, the xx pointer ladder, the literal-gated coercion plan
(isConstString + data_ptr), and the reserved-spelling set. std gains
cstring_len/from_cstring/to_cstring (fmt.sx, re-exported); the old
cstring(size) allocator helper is renamed alloc_string everywhere;
getenv migrates to (name: cstring) -> ?cstring as the canonical user
and env() drops its manual strlen/memcpy.

Pinned: examples/1222 (FFI both directions, literal coercion,
?cstring null paths, round trip) and examples/1173 (both coercion
diagnostics); FAIL pre-feature. The alloc_string rename + getenv
signature shift the .ir snapshots — regenerated. zig build test
426/426; run_examples 604/604.

Spec: reserved spelling + cstring section + C-interop rows.
This commit is contained in:
agra
2026-06-12 14:50:53 +03:00
parent d88bdd7242
commit 1d17b0abcf
58 changed files with 26437 additions and 25257 deletions

View File

@@ -358,6 +358,24 @@ pub const Builder = struct {
/// value and the span it was emitted with; else null. The implicit
/// float→int coercion rule reads this to fold an integral literal to its
/// int (and to locate a non-integral one for its diagnostic).
/// True iff `ref` is a `const_string` instruction — a string LITERAL
/// value (terminated constant data), the only string shape that may
/// implicitly coerce to `cstring`.
pub fn isConstString(self: *Builder, ref: Ref) bool {
if (self.func == null) return false;
const func = self.currentFunc();
const ref_idx = @intFromEnum(ref);
if (ref_idx < func.params.len) return false;
for (func.blocks.items) |*block| {
const first = block.first_ref;
if (ref_idx >= first and ref_idx < first + @as(u32, @intCast(block.insts.items.len))) {
const i = block.insts.items[ref_idx - first];
return i.op == .const_string;
}
}
return false;
}
pub fn constFloatInfo(self: *Builder, ref: Ref) ?ConstFloatInfo {
if (self.func == null) return null;
const func = self.currentFunc();