authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-11 16:07:40-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-11 16:07:40-05:00
log90b8cd4a45bcb2ca131b6ed6466f799aaa162d13
treedc1d0051b504f8fdff0cd677c0e901fc5aca651f
parent342bca7f4627454435e9f6c2d12b099f95a2fd47
signature Commit is signed but in an unrecognized format.

add C pointer type to @typeInfo

See #1059

7 files changed, 41 insertions(+), 6 deletions(-)

src-self-hosted/type.zig+2
...@@ -794,6 +794,7 @@ pub const Type = struct {...@@ -794,6 +794,7 @@ pub const Type = struct {
794 Size.One => "*",794 Size.One => "*",
795 Size.Many => "[*]",795 Size.Many => "[*]",
796 Size.Slice => "[]",796 Size.Slice => "[]",
797 Size.C => "[*c]",
797 };798 };
798 const mut_str = switch (self.key.mut) {799 const mut_str = switch (self.key.mut) {
799 Mut.Const => "const ",800 Mut.Const => "const ",
...@@ -1088,6 +1089,7 @@ fn hashAny(x: var, comptime seed: u64) u32 {...@@ -1088,6 +1089,7 @@ fn hashAny(x: var, comptime seed: u64) u32 {
1088 builtin.TypeInfo.Pointer.Size.One => return hashAny(@ptrToInt(x), seed),1089 builtin.TypeInfo.Pointer.Size.One => return hashAny(@ptrToInt(x), seed),
1089 builtin.TypeInfo.Pointer.Size.Many => @compileError("implement hash function"),1090 builtin.TypeInfo.Pointer.Size.Many => @compileError("implement hash function"),
1090 builtin.TypeInfo.Pointer.Size.Slice => @compileError("implement hash function"),1091 builtin.TypeInfo.Pointer.Size.Slice => @compileError("implement hash function"),
1092 builtin.TypeInfo.Pointer.Size.C => unreachable,
1091 }1093 }
1092 },1094 },
1093 builtin.TypeId.Enum => return hashAny(@enumToInt(x), seed),1095 builtin.TypeId.Enum => return hashAny(@enumToInt(x), seed),
src/codegen.cpp+1
...@@ -7309,6 +7309,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {...@@ -7309,6 +7309,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
7309 " One,\n"7309 " One,\n"
7310 " Many,\n"7310 " Many,\n"
7311 " Slice,\n"7311 " Slice,\n"
7312 " C,\n"
7312 " };\n"7313 " };\n"
7313 " };\n"7314 " };\n"
7314 "\n"7315 "\n"
src/ir.cpp+13-1
...@@ -17584,6 +17584,18 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Sco...@@ -17584,6 +17584,18 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Sco
17584 return ErrorNone;17584 return ErrorNone;
17585}17585}
1758617586
17587static 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
17587static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_type_entry) {17599static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_type_entry) {
17588 Error err;17600 Error err;
17589 ZigType *attrs_type;17601 ZigType *attrs_type;
...@@ -17593,7 +17605,7 @@ static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_ty...@@ -17593,7 +17605,7 @@ static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_ty
17593 size_enum_index = 2;17605 size_enum_index = 2;
17594 } else if (ptr_type_entry->id == ZigTypeIdPointer) {17606 } else if (ptr_type_entry->id == ZigTypeIdPointer) {
17595 attrs_type = ptr_type_entry;17607 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);
17597 } else {17609 } else {
17598 zig_unreachable();17610 zig_unreachable();
17599 }17611 }
std/fmt/index.zig+3
...@@ -236,6 +236,9 @@ pub fn formatType(...@@ -236,6 +236,9 @@ pub fn formatType(
236 const casted_value = ([]const u8)(value);236 const casted_value = ([]const u8)(value);
237 return output(context, casted_value);237 return output(context, casted_value);
238 },238 },
239 builtin.TypeInfo.Pointer.Size.C => {
240 return format(context, Errors, output, "{}@{x}", @typeName(T.Child), @ptrToInt(value));
241 },
239 },242 },
240 builtin.TypeId.Array => |info| {243 builtin.TypeId.Array => |info| {
241 if (info.child == u8) {244 if (info.child == u8) {
std/meta/index.zig+6-3
...@@ -463,13 +463,16 @@ pub fn eql(a: var, b: @typeOf(a)) bool {...@@ -463,13 +463,16 @@ pub fn eql(a: var, b: @typeOf(a)) bool {
463 builtin.TypeId.Pointer => {463 builtin.TypeId.Pointer => {
464 const info = @typeInfo(T).Pointer;464 const info = @typeInfo(T).Pointer;
465 switch (info.size) {465 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,
467 builtin.TypeInfo.Pointer.Size.Slice => return a.ptr == b.ptr and a.len == b.len,470 builtin.TypeInfo.Pointer.Size.Slice => return a.ptr == b.ptr and a.len == b.len,
468 }471 }
469 },472 },
470 builtin.TypeId.Optional => {473 builtin.TypeId.Optional => {
471 if(a == null and b == null) return true;474 if (a == null and b == null) return true;
472 if(a == null or b == null) return false;475 if (a == null or b == null) return false;
473 return eql(a.?, b.?);476 return eql(a.?, b.?);
474 },477 },
475 else => return a == b,478 else => return a == b,
std/testing.zig+1-2
...@@ -69,7 +69,7 @@ pub fn expectEqual(expected: var, actual: var) void {...@@ -69,7 +69,7 @@ pub fn expectEqual(expected: var, actual: var) void {
69 }69 }
70 },70 },
7171
72 builtin.TypeInfo.Pointer.Size.Slice => { 72 builtin.TypeInfo.Pointer.Size.Slice => {
73 if (actual.ptr != expected.ptr) {73 if (actual.ptr != expected.ptr) {
74 std.debug.panic("expected slice ptr {}, found {}", expected.ptr, actual.ptr);74 std.debug.panic("expected slice ptr {}, found {}", expected.ptr, actual.ptr);
75 }75 }
...@@ -122,7 +122,6 @@ pub fn expectEqual(expected: var, actual: var) void {...@@ -122,7 +122,6 @@ pub fn expectEqual(expected: var, actual: var) void {
122 }122 }
123 }123 }
124 },124 },
125
126 }125 }
127}126}
128127
test/stage1/behavior/type_info.zig+15
...@@ -61,6 +61,21 @@ fn testUnknownLenPtr() void {...@@ -61,6 +61,21 @@ fn testUnknownLenPtr() void {
61 expect(u32_ptr_info.Pointer.child == f64);61 expect(u32_ptr_info.Pointer.child == f64);
62}62}
6363
64test "type info: C pointer type info" {
65 testCPtr();
66 comptime testCPtr();
67}
68
69fn 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
64test "type info: slice type info" {79test "type info: slice type info" {
65 testSlice();80 testSlice();
66 comptime testSlice();81 comptime testSlice();