authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-20 13:52:18+03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-20 16:42:48-07:00
log95682484503f031efefb2c085cdd4d0b64cf8795
treee6df9cf657f5e35839d120a0c6ac7709d580c55d
parentebfe723f3cdbb40d2f2280e223b710418abde777

stage2: complex pointer types


5 files changed, 202 insertions(+), 12 deletions(-)

src-self-hosted/Module.zig+33-1
...@@ -3153,7 +3153,7 @@ pub fn simplePtrType(self: *Module, scope: *Scope, src: usize, elem_ty: Type, mu...@@ -3153,7 +3153,7 @@ pub fn simplePtrType(self: *Module, scope: *Scope, src: usize, elem_ty: Type, mu
3153 type_payload.* = .{3153 type_payload.* = .{
3154 .base = .{3154 .base = .{
3155 .tag = switch (size) {3155 .tag = switch (size) {
3156 .One => if (mutable) .single_mut_pointer else T.many_const_pointer,3156 .One => if (mutable) T.single_mut_pointer else T.single_const_pointer,
3157 .Many => if (mutable) T.many_mut_pointer else T.many_const_pointer,3157 .Many => if (mutable) T.many_mut_pointer else T.many_const_pointer,
3158 .C => if (mutable) T.c_mut_pointer else T.c_const_pointer,3158 .C => if (mutable) T.c_mut_pointer else T.c_const_pointer,
3159 else => unreachable,3159 else => unreachable,
...@@ -3164,6 +3164,38 @@ pub fn simplePtrType(self: *Module, scope: *Scope, src: usize, elem_ty: Type, mu...@@ -3164,6 +3164,38 @@ pub fn simplePtrType(self: *Module, scope: *Scope, src: usize, elem_ty: Type, mu
3164 return Type.initPayload(&type_payload.base);3164 return Type.initPayload(&type_payload.base);
3165}3165}
31663166
3167pub fn ptrType(
3168 self: *Module,
3169 scope: *Scope,
3170 src: usize,
3171 elem_ty: Type,
3172 sentinel: ?Value,
3173 @"align": u32,
3174 bit_offset: u16,
3175 host_size: u16,
3176 mutable: bool,
3177 @"allowzero": bool,
3178 @"volatile": bool,
3179 size: std.builtin.TypeInfo.Pointer.Size,
3180) Allocator.Error!Type {
3181 assert(host_size == 0 or bit_offset < host_size * 8);
3182
3183 // TODO check if type can be represented by simplePtrType
3184 const type_payload = try scope.arena().create(Type.Payload.Pointer);
3185 type_payload.* = .{
3186 .pointee_type = elem_ty,
3187 .sentinel = sentinel,
3188 .@"align" = @"align",
3189 .bit_offset = bit_offset,
3190 .host_size = host_size,
3191 .@"allowzero" = @"allowzero",
3192 .mutable = mutable,
3193 .@"volatile" = @"volatile",
3194 .size = size,
3195 };
3196 return Type.initPayload(&type_payload.base);
3197}
3198
3167pub fn optionalType(self: *Module, scope: *Scope, child_type: Type) Allocator.Error!Type {3199pub fn optionalType(self: *Module, scope: *Scope, child_type: Type) Allocator.Error!Type {
3168 return Type.initPayload(switch (child_type.tag()) {3200 return Type.initPayload(switch (child_type.tag()) {
3169 .single_const_pointer => blk: {3201 .single_const_pointer => blk: {
src-self-hosted/astgen.zig+2-3
...@@ -582,8 +582,7 @@ fn ptrType(mod: *Module, scope: *Scope, node: *ast.Node.PtrType) InnerError!*zir...@@ -582,8 +582,7 @@ fn ptrType(mod: *Module, scope: *Scope, node: *ast.Node.PtrType) InnerError!*zir
582 // TODO stage1 type inference bug582 // TODO stage1 type inference bug
583 .LBracket => @as(std.builtin.TypeInfo.Pointer.Size, switch (tree.token_ids[node.op_token + 2]) {583 .LBracket => @as(std.builtin.TypeInfo.Pointer.Size, switch (tree.token_ids[node.op_token + 2]) {
584 .Identifier => .C,584 .Identifier => .C,
585 .RBracket => .Many,585 else => .Many,
586 else => unreachable,
587 }),586 }),
588 else => unreachable,587 else => unreachable,
589 };588 };
...@@ -616,7 +615,7 @@ fn ptrType(mod: *Module, scope: *Scope, node: *ast.Node.PtrType) InnerError!*zir...@@ -616,7 +615,7 @@ fn ptrType(mod: *Module, scope: *Scope, node: *ast.Node.PtrType) InnerError!*zir
616 kw_args.align_bit_end = try expr(mod, scope, .none, bit_range.end);615 kw_args.align_bit_end = try expr(mod, scope, .none, bit_range.end);
617 }616 }
618 }617 }
619 kw_args.@"const" = node.ptr_info.const_token != null;618 kw_args.mutable = node.ptr_info.const_token == null;
620 kw_args.@"volatile" = node.ptr_info.volatile_token != null;619 kw_args.@"volatile" = node.ptr_info.volatile_token != null;
621 if (node.ptr_info.sentinel) |some| {620 if (node.ptr_info.sentinel) |some| {
622 kw_args.sentinel = try expr(mod, scope, .none, some);621 kw_args.sentinel = try expr(mod, scope, .none, some);
src-self-hosted/type.zig+120-6
...@@ -74,6 +74,7 @@ pub const Type = extern union {...@@ -74,6 +74,7 @@ pub const Type = extern union {
74 .many_mut_pointer,74 .many_mut_pointer,
75 .c_const_pointer,75 .c_const_pointer,
76 .c_mut_pointer,76 .c_mut_pointer,
77 .pointer,
77 => return .Pointer,78 => return .Pointer,
7879
79 .optional,80 .optional,
...@@ -390,6 +391,25 @@ pub const Type = extern union {...@@ -390,6 +391,25 @@ pub const Type = extern union {
390 .optional_single_mut_pointer,391 .optional_single_mut_pointer,
391 .optional_single_const_pointer,392 .optional_single_const_pointer,
392 => return self.copyPayloadSingleField(allocator, Payload.PointerSimple, "pointee_type"),393 => return self.copyPayloadSingleField(allocator, Payload.PointerSimple, "pointee_type"),
394
395 .pointer => {
396 const payload = @fieldParentPtr(Payload.Pointer, "base", self.ptr_otherwise);
397 const new_payload = try allocator.create(Payload.Pointer);
398 new_payload.* = .{
399 .base = payload.base,
400
401 .pointee_type = try payload.pointee_type.copy(allocator),
402 .sentinel = if (payload.sentinel) |some| try some.copy(allocator) else null,
403 .@"align" = payload.@"align",
404 .bit_offset = payload.bit_offset,
405 .host_size = payload.host_size,
406 .@"allowzero" = payload.@"allowzero",
407 .mutable = payload.mutable,
408 .@"volatile" = payload.@"volatile",
409 .size = payload.size,
410 };
411 return Type{ .ptr_otherwise = &new_payload.base };
412 },
393 }413 }
394 }414 }
395415
...@@ -556,6 +576,34 @@ pub const Type = extern union {...@@ -556,6 +576,34 @@ pub const Type = extern union {
556 ty = payload.pointee_type;576 ty = payload.pointee_type;
557 continue;577 continue;
558 },578 },
579
580 .pointer => {
581 const payload = @fieldParentPtr(Payload.Pointer, "base", ty.ptr_otherwise);
582 if (payload.sentinel) |some| switch (payload.size) {
583 .One, .C => unreachable,
584 .Many => try out_stream.writeAll("[*:{}]"),
585 .Slice => try out_stream.writeAll("[:{}]"),
586 } else switch (payload.size) {
587 .One => try out_stream.writeAll("*"),
588 .Many => try out_stream.writeAll("[*]"),
589 .C => try out_stream.writeAll("[*c]"),
590 .Slice => try out_stream.writeAll("[]"),
591 }
592 if (payload.@"align" != 0) {
593 try out_stream.print("align({}", .{payload.@"align"});
594
595 if (payload.bit_offset != 0) {
596 try out_stream.print(":{}:{}", .{ payload.bit_offset, payload.host_size });
597 }
598 try out_stream.writeAll(") ");
599 }
600 if (!payload.mutable) try out_stream.writeAll("const ");
601 if (payload.@"volatile") try out_stream.writeAll("volatile ");
602 if (payload.@"allowzero") try out_stream.writeAll("allowzero ");
603
604 ty = payload.pointee_type;
605 continue;
606 },
559 }607 }
560 unreachable;608 unreachable;
561 }609 }
...@@ -660,6 +708,7 @@ pub const Type = extern union {...@@ -660,6 +708,7 @@ pub const Type = extern union {
660 .many_mut_pointer => self.elemType().hasCodeGenBits(),708 .many_mut_pointer => self.elemType().hasCodeGenBits(),
661 .c_const_pointer => self.elemType().hasCodeGenBits(),709 .c_const_pointer => self.elemType().hasCodeGenBits(),
662 .c_mut_pointer => self.elemType().hasCodeGenBits(),710 .c_mut_pointer => self.elemType().hasCodeGenBits(),
711 .pointer => self.elemType().hasCodeGenBits(),
663 .int_signed => self.cast(Payload.IntSigned).?.bits == 0,712 .int_signed => self.cast(Payload.IntSigned).?.bits == 0,
664 .int_unsigned => self.cast(Payload.IntUnsigned).?.bits == 0,713 .int_unsigned => self.cast(Payload.IntUnsigned).?.bits == 0,
665714
...@@ -718,6 +767,13 @@ pub const Type = extern union {...@@ -718,6 +767,13 @@ pub const Type = extern union {
718 .optional_single_mut_pointer,767 .optional_single_mut_pointer,
719 => return @divExact(target.cpu.arch.ptrBitWidth(), 8),768 => return @divExact(target.cpu.arch.ptrBitWidth(), 8),
720769
770 .pointer => {
771 const payload = @fieldParentPtr(Payload.Pointer, "base", self.ptr_otherwise);
772
773 if (payload.@"align" != 0) return payload.@"align";
774 return @divExact(target.cpu.arch.ptrBitWidth(), 8);
775 },
776
721 .c_short => return @divExact(CType.short.sizeInBits(target), 8),777 .c_short => return @divExact(CType.short.sizeInBits(target), 8),
722 .c_ushort => return @divExact(CType.ushort.sizeInBits(target), 8),778 .c_ushort => return @divExact(CType.ushort.sizeInBits(target), 8),
723 .c_int => return @divExact(CType.int.sizeInBits(target), 8),779 .c_int => return @divExact(CType.int.sizeInBits(target), 8),
...@@ -789,6 +845,7 @@ pub const Type = extern union {...@@ -789,6 +845,7 @@ pub const Type = extern union {
789 .@"null" => unreachable,845 .@"null" => unreachable,
790 .@"undefined" => unreachable,846 .@"undefined" => unreachable,
791 .enum_literal => unreachable,847 .enum_literal => unreachable,
848 .single_const_pointer_to_comptime_int => unreachable,
792849
793 .u8,850 .u8,
794 .i8,851 .i8,
...@@ -812,18 +869,28 @@ pub const Type = extern union {...@@ -812,18 +869,28 @@ pub const Type = extern union {
812 .i64, .u64 => return 8,869 .i64, .u64 => return 8,
813870
814 .isize,871 .isize,
815 .usize,872 .usize
816 .single_const_pointer_to_comptime_int,873 => return @divExact(target.cpu.arch.ptrBitWidth(), 8),
817 .const_slice_u8,874 .const_slice_u8 => return @divExact(target.cpu.arch.ptrBitWidth(), 8) * 2,
875
876 .optional_single_const_pointer,
877 .optional_single_mut_pointer,
878 => {
879 if (self.elemType().hasCodeGenBits()) return 1;
880 return @divExact(target.cpu.arch.ptrBitWidth(), 8);
881 },
882
818 .single_const_pointer,883 .single_const_pointer,
819 .single_mut_pointer,884 .single_mut_pointer,
820 .many_const_pointer,885 .many_const_pointer,
821 .many_mut_pointer,886 .many_mut_pointer,
822 .c_const_pointer,887 .c_const_pointer,
823 .c_mut_pointer,888 .c_mut_pointer,
824 .optional_single_const_pointer,889 .pointer,
825 .optional_single_mut_pointer,890 => {
826 => return @divExact(target.cpu.arch.ptrBitWidth(), 8),891 if (self.elemType().hasCodeGenBits()) return 0;
892 return @divExact(target.cpu.arch.ptrBitWidth(), 8);
893 },
827894
828 .c_short => return @divExact(CType.short.sizeInBits(target), 8),895 .c_short => return @divExact(CType.short.sizeInBits(target), 8),
829 .c_ushort => return @divExact(CType.ushort.sizeInBits(target), 8),896 .c_ushort => return @divExact(CType.ushort.sizeInBits(target), 8),
...@@ -931,6 +998,8 @@ pub const Type = extern union {...@@ -931,6 +998,8 @@ pub const Type = extern union {
931 .single_mut_pointer,998 .single_mut_pointer,
932 .single_const_pointer_to_comptime_int,999 .single_const_pointer_to_comptime_int,
933 => true,1000 => true,
1001
1002 .pointer => self.cast(Payload.Pointer).?.size == .One,
934 };1003 };
935 }1004 }
9361005
...@@ -994,6 +1063,8 @@ pub const Type = extern union {...@@ -994,6 +1063,8 @@ pub const Type = extern union {
994 => false,1063 => false,
9951064
996 .const_slice_u8 => true,1065 .const_slice_u8 => true,
1066
1067 .pointer => self.cast(Payload.Pointer).?.size == .Slice,
997 };1068 };
998 }1069 }
9991070
...@@ -1058,6 +1129,8 @@ pub const Type = extern union {...@@ -1058,6 +1129,8 @@ pub const Type = extern union {
1058 .single_const_pointer_to_comptime_int,1129 .single_const_pointer_to_comptime_int,
1059 .const_slice_u8,1130 .const_slice_u8,
1060 => true,1131 => true,
1132
1133 .pointer => !self.cast(Payload.Pointer).?.mutable,
1061 };1134 };
1062 }1135 }
10631136
...@@ -1120,6 +1193,11 @@ pub const Type = extern union {...@@ -1120,6 +1193,11 @@ pub const Type = extern union {
1120 .optional_single_const_pointer,1193 .optional_single_const_pointer,
1121 .enum_literal,1194 .enum_literal,
1122 => false,1195 => false,
1196
1197 .pointer => {
1198 const payload = @fieldParentPtr(Payload.Pointer, "base", self.ptr_otherwise);
1199 return payload.@"volatile";
1200 },
1123 };1201 };
1124 }1202 }
11251203
...@@ -1237,6 +1315,7 @@ pub const Type = extern union {...@@ -1237,6 +1315,7 @@ pub const Type = extern union {
1237 .c_mut_pointer => self.castPointer().?.pointee_type,1315 .c_mut_pointer => self.castPointer().?.pointee_type,
1238 .array_u8, .array_u8_sentinel_0, .const_slice_u8 => Type.initTag(.u8),1316 .array_u8, .array_u8_sentinel_0, .const_slice_u8 => Type.initTag(.u8),
1239 .single_const_pointer_to_comptime_int => Type.initTag(.comptime_int),1317 .single_const_pointer_to_comptime_int => Type.initTag(.comptime_int),
1318 .pointer => self.cast(Payload.Pointer).?.pointee_type,
1240 };1319 };
1241 }1320 }
12421321
...@@ -1325,6 +1404,7 @@ pub const Type = extern union {...@@ -1325,6 +1404,7 @@ pub const Type = extern union {
1325 .fn_naked_noreturn_no_args,1404 .fn_naked_noreturn_no_args,
1326 .fn_ccc_void_no_args,1405 .fn_ccc_void_no_args,
1327 .function,1406 .function,
1407 .pointer,
1328 .single_const_pointer,1408 .single_const_pointer,
1329 .single_mut_pointer,1409 .single_mut_pointer,
1330 .many_const_pointer,1410 .many_const_pointer,
...@@ -1389,6 +1469,7 @@ pub const Type = extern union {...@@ -1389,6 +1469,7 @@ pub const Type = extern union {
1389 .fn_naked_noreturn_no_args,1469 .fn_naked_noreturn_no_args,
1390 .fn_ccc_void_no_args,1470 .fn_ccc_void_no_args,
1391 .function,1471 .function,
1472 .pointer,
1392 .single_const_pointer,1473 .single_const_pointer,
1393 .single_mut_pointer,1474 .single_mut_pointer,
1394 .many_const_pointer,1475 .many_const_pointer,
...@@ -1443,6 +1524,7 @@ pub const Type = extern union {...@@ -1443,6 +1524,7 @@ pub const Type = extern union {
1443 .array_sentinel,1524 .array_sentinel,
1444 .array_u8,1525 .array_u8,
1445 .array_u8_sentinel_0,1526 .array_u8_sentinel_0,
1527 .pointer,
1446 .single_const_pointer,1528 .single_const_pointer,
1447 .single_mut_pointer,1529 .single_mut_pointer,
1448 .many_const_pointer,1530 .many_const_pointer,
...@@ -1508,6 +1590,7 @@ pub const Type = extern union {...@@ -1508,6 +1590,7 @@ pub const Type = extern union {
1508 .array_sentinel,1590 .array_sentinel,
1509 .array_u8,1591 .array_u8,
1510 .array_u8_sentinel_0,1592 .array_u8_sentinel_0,
1593 .pointer,
1511 .single_const_pointer,1594 .single_const_pointer,
1512 .single_mut_pointer,1595 .single_mut_pointer,
1513 .many_const_pointer,1596 .many_const_pointer,
...@@ -1573,6 +1656,7 @@ pub const Type = extern union {...@@ -1573,6 +1656,7 @@ pub const Type = extern union {
1573 .array_sentinel,1656 .array_sentinel,
1574 .array_u8,1657 .array_u8,
1575 .array_u8_sentinel_0,1658 .array_u8_sentinel_0,
1659 .pointer,
1576 .single_const_pointer,1660 .single_const_pointer,
1577 .single_mut_pointer,1661 .single_mut_pointer,
1578 .many_const_pointer,1662 .many_const_pointer,
...@@ -1636,6 +1720,7 @@ pub const Type = extern union {...@@ -1636,6 +1720,7 @@ pub const Type = extern union {
1636 .array_sentinel,1720 .array_sentinel,
1637 .array_u8,1721 .array_u8,
1638 .array_u8_sentinel_0,1722 .array_u8_sentinel_0,
1723 .pointer,
1639 .single_const_pointer,1724 .single_const_pointer,
1640 .single_mut_pointer,1725 .single_mut_pointer,
1641 .many_const_pointer,1726 .many_const_pointer,
...@@ -1728,6 +1813,7 @@ pub const Type = extern union {...@@ -1728,6 +1813,7 @@ pub const Type = extern union {
1728 .array_sentinel,1813 .array_sentinel,
1729 .array_u8,1814 .array_u8,
1730 .array_u8_sentinel_0,1815 .array_u8_sentinel_0,
1816 .pointer,
1731 .single_const_pointer,1817 .single_const_pointer,
1732 .single_mut_pointer,1818 .single_mut_pointer,
1733 .many_const_pointer,1819 .many_const_pointer,
...@@ -1796,6 +1882,7 @@ pub const Type = extern union {...@@ -1796,6 +1882,7 @@ pub const Type = extern union {
1796 .array_sentinel,1882 .array_sentinel,
1797 .array_u8,1883 .array_u8,
1798 .array_u8_sentinel_0,1884 .array_u8_sentinel_0,
1885 .pointer,
1799 .single_const_pointer,1886 .single_const_pointer,
1800 .single_mut_pointer,1887 .single_mut_pointer,
1801 .many_const_pointer,1888 .many_const_pointer,
...@@ -1863,6 +1950,7 @@ pub const Type = extern union {...@@ -1863,6 +1950,7 @@ pub const Type = extern union {
1863 .array_sentinel,1950 .array_sentinel,
1864 .array_u8,1951 .array_u8,
1865 .array_u8_sentinel_0,1952 .array_u8_sentinel_0,
1953 .pointer,
1866 .single_const_pointer,1954 .single_const_pointer,
1867 .single_mut_pointer,1955 .single_mut_pointer,
1868 .many_const_pointer,1956 .many_const_pointer,
...@@ -1930,6 +2018,7 @@ pub const Type = extern union {...@@ -1930,6 +2018,7 @@ pub const Type = extern union {
1930 .array_sentinel,2018 .array_sentinel,
1931 .array_u8,2019 .array_u8,
1932 .array_u8_sentinel_0,2020 .array_u8_sentinel_0,
2021 .pointer,
1933 .single_const_pointer,2022 .single_const_pointer,
1934 .single_mut_pointer,2023 .single_mut_pointer,
1935 .many_const_pointer,2024 .many_const_pointer,
...@@ -1994,6 +2083,7 @@ pub const Type = extern union {...@@ -1994,6 +2083,7 @@ pub const Type = extern union {
1994 .array_sentinel,2083 .array_sentinel,
1995 .array_u8,2084 .array_u8,
1996 .array_u8_sentinel_0,2085 .array_u8_sentinel_0,
2086 .pointer,
1997 .single_const_pointer,2087 .single_const_pointer,
1998 .single_mut_pointer,2088 .single_mut_pointer,
1999 .many_const_pointer,2089 .many_const_pointer,
...@@ -2058,6 +2148,7 @@ pub const Type = extern union {...@@ -2058,6 +2148,7 @@ pub const Type = extern union {
2058 .array_sentinel,2148 .array_sentinel,
2059 .array_u8,2149 .array_u8,
2060 .array_u8_sentinel_0,2150 .array_u8_sentinel_0,
2151 .pointer,
2061 .single_const_pointer,2152 .single_const_pointer,
2062 .single_mut_pointer,2153 .single_mut_pointer,
2063 .many_const_pointer,2154 .many_const_pointer,
...@@ -2142,6 +2233,7 @@ pub const Type = extern union {...@@ -2142,6 +2233,7 @@ pub const Type = extern union {
2142 .array_sentinel,2233 .array_sentinel,
2143 .array_u8,2234 .array_u8,
2144 .array_u8_sentinel_0,2235 .array_u8_sentinel_0,
2236 .pointer,
2145 .single_const_pointer,2237 .single_const_pointer,
2146 .single_mut_pointer,2238 .single_mut_pointer,
2147 .many_const_pointer,2239 .many_const_pointer,
...@@ -2241,6 +2333,10 @@ pub const Type = extern union {...@@ -2241,6 +2333,10 @@ pub const Type = extern union {
2241 ty = ptr.pointee_type;2333 ty = ptr.pointee_type;
2242 continue;2334 continue;
2243 },2335 },
2336 .pointer => {
2337 ty = ty.cast(Payload.Pointer).?.pointee_type;
2338 continue;
2339 },
2244 };2340 };
2245 }2341 }
22462342
...@@ -2305,6 +2401,8 @@ pub const Type = extern union {...@@ -2305,6 +2401,8 @@ pub const Type = extern union {
2305 .c_const_pointer,2401 .c_const_pointer,
2306 .c_mut_pointer,2402 .c_mut_pointer,
2307 => return true,2403 => return true,
2404
2405 .pointer => self.cast(Payload.Pointer).?.size == .C,
2308 };2406 };
2309 }2407 }
23102408
...@@ -2362,6 +2460,7 @@ pub const Type = extern union {...@@ -2362,6 +2460,7 @@ pub const Type = extern union {
2362 array_u8_sentinel_0,2460 array_u8_sentinel_0,
2363 array,2461 array,
2364 array_sentinel,2462 array_sentinel,
2463 pointer,
2365 single_const_pointer,2464 single_const_pointer,
2366 single_mut_pointer,2465 single_mut_pointer,
2367 many_const_pointer,2466 many_const_pointer,
...@@ -2440,6 +2539,21 @@ pub const Type = extern union {...@@ -2440,6 +2539,21 @@ pub const Type = extern union {
24402539
2441 child_type: Type,2540 child_type: Type,
2442 };2541 };
2542
2543 pub const Pointer = struct {
2544 base: Payload = .{ .tag = .pointer },
2545
2546 pointee_type: Type,
2547 sentinel: ?Value,
2548 /// If zero use pointee_type.AbiAlign()
2549 @"align": u32,
2550 bit_offset: u16,
2551 host_size: u16,
2552 @"allowzero": bool,
2553 mutable: bool,
2554 @"volatile": bool,
2555 size: std.builtin.TypeInfo.Pointer.Size,
2556 };
2443 };2557 };
2444};2558};
24452559
src-self-hosted/zir.zig+1-1
...@@ -872,7 +872,7 @@ pub const Inst = struct {...@@ -872,7 +872,7 @@ pub const Inst = struct {
872 @"align": ?*Inst = null,872 @"align": ?*Inst = null,
873 align_bit_start: ?*Inst = null,873 align_bit_start: ?*Inst = null,
874 align_bit_end: ?*Inst = null,874 align_bit_end: ?*Inst = null,
875 @"const": bool = true,875 mutable: bool = true,
876 @"volatile": bool = false,876 @"volatile": bool = false,
877 sentinel: ?*Inst = null,877 sentinel: ?*Inst = null,
878 size: std.builtin.TypeInfo.Pointer.Size = .One,878 size: std.builtin.TypeInfo.Pointer.Size = .One,
src-self-hosted/zir_sema.zig+46-1
...@@ -271,6 +271,14 @@ fn resolveType(mod: *Module, scope: *Scope, old_inst: *zir.Inst) !Type {...@@ -271,6 +271,14 @@ fn resolveType(mod: *Module, scope: *Scope, old_inst: *zir.Inst) !Type {
271 return val.toType();271 return val.toType();
272}272}
273273
274fn resolveInt(mod: *Module, scope: *Scope, old_inst: *zir.Inst, dest_type: Type) !u64 {
275 const new_inst = try resolveInst(mod, scope, old_inst);
276 const coerced = try mod.coerce(scope, dest_type, new_inst);
277 const val = try mod.resolveConstValue(scope, coerced);
278
279 return val.toUnsignedInt();
280}
281
274pub fn resolveInstConst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!TypedValue {282pub fn resolveInstConst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!TypedValue {
275 const new_inst = try resolveInst(mod, scope, old_inst);283 const new_inst = try resolveInst(mod, scope, old_inst);
276 const val = try mod.resolveConstValue(scope, new_inst);284 const val = try mod.resolveConstValue(scope, new_inst);
...@@ -1322,5 +1330,42 @@ fn analyzeInstSimplePtrType(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp, m...@@ -1322,5 +1330,42 @@ fn analyzeInstSimplePtrType(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp, m
1322}1330}
13231331
1324fn analyzeInstPtrType(mod: *Module, scope: *Scope, inst: *zir.Inst.PtrType) InnerError!*Inst {1332fn analyzeInstPtrType(mod: *Module, scope: *Scope, inst: *zir.Inst.PtrType) InnerError!*Inst {
1325 return mod.fail(scope, inst.base.src, "TODO implement ptr_type", .{});1333 // TODO lazy values
1334 const @"align" = if (inst.kw_args.@"align") |some|
1335 @truncate(u32, try resolveInt(mod, scope, some, Type.initTag(.u32)))
1336 else
1337 0;
1338 const bit_offset = if (inst.kw_args.align_bit_start) |some|
1339 @truncate(u16, try resolveInt(mod, scope, some, Type.initTag(.u16)))
1340 else
1341 0;
1342 const host_size = if (inst.kw_args.align_bit_end) |some|
1343 @truncate(u16, try resolveInt(mod, scope, some, Type.initTag(.u16)))
1344 else
1345 0;
1346
1347 if (host_size != 0 and bit_offset >= host_size * 8)
1348 return mod.fail(scope, inst.base.src, "bit offset starts after end of host integer", .{});
1349
1350 const sentinel = if (inst.kw_args.sentinel) |some|
1351 (try resolveInstConst(mod, scope, some)).val
1352 else
1353 null;
1354
1355 const elem_type = try resolveType(mod, scope, inst.positionals.child_type);
1356
1357 const ty = try mod.ptrType(
1358 scope,
1359 inst.base.src,
1360 elem_type,
1361 sentinel,
1362 @"align",
1363 bit_offset,
1364 host_size,
1365 inst.kw_args.mutable,
1366 inst.kw_args.@"allowzero",
1367 inst.kw_args.@"volatile",
1368 inst.kw_args.size,
1369 );
1370 return mod.constType(scope, inst.base.src, ty);
1326}1371}