C backend: Improve lowering of Zig types to C types
1. Changed Zig pointers to functions to be typedef'd so then we can treat them the same as other types. 2. Distinguished between const slices (zig_L prefix) and mut slices (zig_M prefix). 3. Changed lowering of Zig "const pointers" (e.g. *const u8) to to C "pointers to const" (e.g. const char *) rather than C "const pointers" (e.g. char * const) 4. Ensured that all typedefs are "linked" even if the decl doesn't require any forward declarations 5. Added test that exercises function pointer type rendering 6. Changed .slice_ptr instruction to allocate pointer local rather than a uintptr_t local
This commit is contained in:
committed by
Andrew Kelley
parent
91c3206b45
commit
51efd553ae
@@ -250,3 +250,45 @@ test "volatile load and store" {
|
||||
ptr.* += 1;
|
||||
try expect(ptr.* == 1235);
|
||||
}
|
||||
|
||||
fn fA() []const u8 {
|
||||
return "a";
|
||||
}
|
||||
fn fB() []const u8 {
|
||||
return "b";
|
||||
}
|
||||
|
||||
test "call function pointer in struct" {
|
||||
try expect(mem.eql(u8, f3(true), "a"));
|
||||
try expect(mem.eql(u8, f3(false), "b"));
|
||||
}
|
||||
|
||||
fn f3(x: bool) []const u8 {
|
||||
var wrapper: FnPtrWrapper = .{
|
||||
.fn_ptr = fB,
|
||||
};
|
||||
|
||||
if (x) {
|
||||
wrapper.fn_ptr = fA;
|
||||
}
|
||||
|
||||
return wrapper.fn_ptr();
|
||||
}
|
||||
|
||||
const FnPtrWrapper = struct {
|
||||
fn_ptr: fn () []const u8,
|
||||
};
|
||||
|
||||
test "const ptr from var variable" {
|
||||
var x: u64 = undefined;
|
||||
var y: u64 = undefined;
|
||||
|
||||
x = 78;
|
||||
copy(&x, &y);
|
||||
|
||||
try expect(x == y);
|
||||
}
|
||||
|
||||
fn copy(src: *const u64, dst: *u64) void {
|
||||
dst.* = src.*;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user