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:
@@ -20,6 +20,7 @@ pub const Type = union(enum) {
|
||||
void_type,
|
||||
boolean,
|
||||
string_type,
|
||||
cstring_type,
|
||||
enum_type: []const u8,
|
||||
struct_type: []const u8,
|
||||
union_type: []const u8,
|
||||
@@ -116,6 +117,7 @@ pub const Type = union(enum) {
|
||||
if (name.len == 0) return null;
|
||||
return switch (name[0]) {
|
||||
's' => if (std.mem.eql(u8, name, "string")) .string_type else null,
|
||||
'c' => if (std.mem.eql(u8, name, "cstring")) .cstring_type else null,
|
||||
'u' => {
|
||||
if (std.mem.eql(u8, name, "usize")) return .usize_type;
|
||||
if (name.len >= 2) {
|
||||
@@ -199,6 +201,7 @@ pub const Type = union(enum) {
|
||||
.f64 => "f64",
|
||||
.boolean => "bool",
|
||||
.string_type => "string",
|
||||
.cstring_type => "cstring",
|
||||
.void_type => "void",
|
||||
.usize_type => "usize",
|
||||
.isize_type => "isize",
|
||||
@@ -286,6 +289,7 @@ pub const Type = union(enum) {
|
||||
.f64 => "f64",
|
||||
.boolean => "bool",
|
||||
.string_type => "string",
|
||||
.cstring_type => "cstring",
|
||||
.void_type => "void",
|
||||
.any_type => "Any",
|
||||
.usize_type => "usize",
|
||||
|
||||
Reference in New Issue
Block a user