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:
@@ -48,7 +48,7 @@ declare ptr @memset(ptr, i32, i64)
|
||||
declare void @out.1(ptr) #0
|
||||
|
||||
; Function Attrs: nounwind
|
||||
define internal { ptr, i64 } @cstring(ptr %0, i64 %1) #0 {
|
||||
define internal { ptr, i64 } @alloc_string(ptr %0, i64 %1) #0 {
|
||||
entry:
|
||||
%alloca = alloca i64, align 8
|
||||
store i64 %1, ptr %alloca, align 8
|
||||
@@ -99,7 +99,7 @@ if.merge.15: ; preds = %entry
|
||||
%loadN = load i64, ptr %alloca, align 8
|
||||
%icmpN = icmp slt i64 %loadN, 0
|
||||
store i1 %icmpN, ptr %allocaN, align 1
|
||||
%call = call { ptr, i64 } @cstring(ptr %0, i64 20)
|
||||
%call = call { ptr, i64 } @alloc_string(ptr %0, i64 20)
|
||||
store { ptr, i64 } %call, ptr %allocaN, align 8
|
||||
store i64 19, ptr %allocaN, align 8
|
||||
%loadN = load i64, ptr %alloca, align 8
|
||||
@@ -203,7 +203,7 @@ entry:
|
||||
%loadN = load i64, ptr %allocaN, align 8
|
||||
%loadN = load i64, ptr %allocaN, align 8
|
||||
%add = add i64 %loadN, %loadN
|
||||
%call = call { ptr, i64 } @cstring(ptr %0, i64 %add)
|
||||
%call = call { ptr, i64 } @alloc_string(ptr %0, i64 %add)
|
||||
%allocaN = alloca { ptr, i64 }, align 8
|
||||
store { ptr, i64 } %call, ptr %allocaN, align 8
|
||||
%loadN = load { ptr, i64 }, ptr %allocaN, align 8
|
||||
@@ -234,7 +234,7 @@ entry:
|
||||
%allocaN = alloca i64, align 8
|
||||
store i64 %3, ptr %allocaN, align 8
|
||||
%load = load i64, ptr %allocaN, align 8
|
||||
%call = call { ptr, i64 } @cstring(ptr %0, i64 %load)
|
||||
%call = call { ptr, i64 } @alloc_string(ptr %0, i64 %load)
|
||||
%allocaN = alloca { ptr, i64 }, align 8
|
||||
store { ptr, i64 } %call, ptr %allocaN, align 8
|
||||
%loadN = load { ptr, i64 }, ptr %allocaN, align 8
|
||||
@@ -249,6 +249,15 @@ entry:
|
||||
ret { ptr, i64 } %loadN
|
||||
}
|
||||
|
||||
; Function Attrs: nounwind
|
||||
declare i64 @cstring_len(ptr, ptr) #0
|
||||
|
||||
; Function Attrs: nounwind
|
||||
declare ptr @from_cstring(ptr, ptr) #0
|
||||
|
||||
; Function Attrs: nounwind
|
||||
declare ptr @to_cstring(ptr, ptr) #0
|
||||
|
||||
; Function Attrs: nounwind
|
||||
declare ptr @path_join(ptr, ptr) #0
|
||||
|
||||
@@ -758,9 +767,6 @@ declare i32 @feof(ptr) #0
|
||||
; Function Attrs: nounwind
|
||||
declare ptr @getenv(ptr) #0
|
||||
|
||||
; Function Attrs: nounwind
|
||||
declare i64 @strlen(ptr) #0
|
||||
|
||||
; Function Attrs: nounwind
|
||||
declare i32 @system(ptr) #0
|
||||
|
||||
|
||||
Reference in New Issue
Block a user