Skip to content

Commit 90b8cd4

Browse files
committed
add C pointer type to @typeinfo
See #1059
1 parent 342bca7 commit 90b8cd4

File tree

7 files changed

+41
-6
lines changed

7 files changed

+41
-6
lines changed

src-self-hosted/type.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,7 @@ pub const Type = struct {
794794
Size.One => "*",
795795
Size.Many => "[*]",
796796
Size.Slice => "[]",
797+
Size.C => "[*c]",
797798
};
798799
const mut_str = switch (self.key.mut) {
799800
Mut.Const => "const ",
@@ -1088,6 +1089,7 @@ fn hashAny(x: var, comptime seed: u64) u32 {
10881089
builtin.TypeInfo.Pointer.Size.One => return hashAny(@ptrToInt(x), seed),
10891090
builtin.TypeInfo.Pointer.Size.Many => @compileError("implement hash function"),
10901091
builtin.TypeInfo.Pointer.Size.Slice => @compileError("implement hash function"),
1092+
builtin.TypeInfo.Pointer.Size.C => unreachable,
10911093
}
10921094
},
10931095
builtin.TypeId.Enum => return hashAny(@enumToInt(x), seed),

src/codegen.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7309,6 +7309,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
73097309
" One,\n"
73107310
" Many,\n"
73117311
" Slice,\n"
7312+
" C,\n"
73127313
" };\n"
73137314
" };\n"
73147315
"\n"

src/ir.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17584,6 +17584,18 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Sco
1758417584
return ErrorNone;
1758517585
}
1758617586

17587+
static uint32_t ptr_len_to_size_enum_index(PtrLen ptr_len) {
17588+
switch (ptr_len) {
17589+
case PtrLenSingle:
17590+
return 0;
17591+
case PtrLenUnknown:
17592+
return 1;
17593+
case PtrLenC:
17594+
return 3;
17595+
}
17596+
zig_unreachable();
17597+
}
17598+
1758717599
static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_type_entry) {
1758817600
Error err;
1758917601
ZigType *attrs_type;
@@ -17593,7 +17605,7 @@ static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_ty
1759317605
size_enum_index = 2;
1759417606
} else if (ptr_type_entry->id == ZigTypeIdPointer) {
1759517607
attrs_type = ptr_type_entry;
17596-
size_enum_index = (ptr_type_entry->data.pointer.ptr_len == PtrLenSingle) ? 0 : 1;
17608+
size_enum_index = ptr_len_to_size_enum_index(ptr_type_entry->data.pointer.ptr_len);
1759717609
} else {
1759817610
zig_unreachable();
1759917611
}

std/fmt/index.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ pub fn formatType(
236236
const casted_value = ([]const u8)(value);
237237
return output(context, casted_value);
238238
},
239+
builtin.TypeInfo.Pointer.Size.C => {
240+
return format(context, Errors, output, "{}@{x}", @typeName(T.Child), @ptrToInt(value));
241+
},
239242
},
240243
builtin.TypeId.Array => |info| {
241244
if (info.child == u8) {

std/meta/index.zig

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,13 +463,16 @@ pub fn eql(a: var, b: @typeOf(a)) bool {
463463
builtin.TypeId.Pointer => {
464464
const info = @typeInfo(T).Pointer;
465465
switch (info.size) {
466-
builtin.TypeInfo.Pointer.Size.One, builtin.TypeInfo.Pointer.Size.Many => return a == b,
466+
builtin.TypeInfo.Pointer.Size.One,
467+
builtin.TypeInfo.Pointer.Size.Many,
468+
builtin.TypeInfo.Pointer.Size.C,
469+
=> return a == b,
467470
builtin.TypeInfo.Pointer.Size.Slice => return a.ptr == b.ptr and a.len == b.len,
468471
}
469472
},
470473
builtin.TypeId.Optional => {
471-
if(a == null and b == null) return true;
472-
if(a == null or b == null) return false;
474+
if (a == null and b == null) return true;
475+
if (a == null or b == null) return false;
473476
return eql(a.?, b.?);
474477
},
475478
else => return a == b,

std/testing.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn expectEqual(expected: var, actual: var) void {
6969
}
7070
},
7171

72-
builtin.TypeInfo.Pointer.Size.Slice => {
72+
builtin.TypeInfo.Pointer.Size.Slice => {
7373
if (actual.ptr != expected.ptr) {
7474
std.debug.panic("expected slice ptr {}, found {}", expected.ptr, actual.ptr);
7575
}
@@ -122,7 +122,6 @@ pub fn expectEqual(expected: var, actual: var) void {
122122
}
123123
}
124124
},
125-
126125
}
127126
}
128127

test/stage1/behavior/type_info.zig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ fn testUnknownLenPtr() void {
6161
expect(u32_ptr_info.Pointer.child == f64);
6262
}
6363

64+
test "type info: C pointer type info" {
65+
testCPtr();
66+
comptime testCPtr();
67+
}
68+
69+
fn testCPtr() void {
70+
const ptr_info = @typeInfo([*c]align(4) const i8);
71+
expect(TypeId(ptr_info) == TypeId.Pointer);
72+
expect(ptr_info.Pointer.size == TypeInfo.Pointer.Size.C);
73+
expect(ptr_info.Pointer.is_const);
74+
expect(!ptr_info.Pointer.is_volatile);
75+
expect(ptr_info.Pointer.alignment == 4);
76+
expect(ptr_info.Pointer.child == i8);
77+
}
78+
6479
test "type info: slice type info" {
6580
testSlice();
6681
comptime testSlice();

0 commit comments

Comments
 (0)