authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-20 12:20:24+03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-20 16:42:48-07:00
logebfe723f3cdbb40d2f2280e223b710418abde777
treec5cdc3061e3b8b7c84f34a14866871a7216c730b
parenteef111fe78d7b246b634a07561082a6fcf947e09

stage2: implement rest of simple pointer types


6 files changed, 230 insertions(+), 51 deletions(-)

src-self-hosted/Module.zig+18-8
......@@ -2429,7 +2429,7 @@ pub fn analyzeDeclRef(self: *Module, scope: *Scope, src: usize, decl: *Decl) Inn
24292429 if (decl_tv.val.tag() == .variable) {
24302430 return self.analyzeVarRef(scope, src, decl_tv);
24312431 }
2432 const ty = try self.singlePtrType(scope, src, false, decl_tv.ty);
2432 const ty = try self.simplePtrType(scope, src, decl_tv.ty, false, .One);
24332433 const val_payload = try scope.arena().create(Value.Payload.DeclRef);
24342434 val_payload.* = .{ .decl = decl };
24352435
......@@ -2442,7 +2442,7 @@ pub fn analyzeDeclRef(self: *Module, scope: *Scope, src: usize, decl: *Decl) Inn
24422442fn analyzeVarRef(self: *Module, scope: *Scope, src: usize, tv: TypedValue) InnerError!*Inst {
24432443 const variable = tv.val.cast(Value.Payload.Variable).?.variable;
24442444
2445 const ty = try self.singlePtrType(scope, src, variable.is_mutable, tv.ty);
2445 const ty = try self.singlePtrType(scope, src, variable.is_mutable, tv.ty, .One);
24462446 if (!variable.is_mutable and !variable.is_extern) {
24472447 const val_payload = try scope.arena().create(Value.Payload.RefVal);
24482448 val_payload.* = .{ .val = variable.init };
......@@ -2766,7 +2766,7 @@ pub fn coerce(self: *Module, scope: *Scope, dest_type: Type, inst: *Inst) !*Inst
27662766
27672767 // T to ?T
27682768 if (dest_type.zigTypeTag() == .Optional) {
2769 var buf: Type.Payload.Pointer = undefined;
2769 var buf: Type.Payload.PointerSimple = undefined;
27702770 const child_type = dest_type.optionalChild(&buf);
27712771 if (child_type.eql(inst.ty)) {
27722772 return self.wrapOptional(scope, dest_type, inst);
......@@ -3145,10 +3145,20 @@ pub fn floatSub(self: *Module, scope: *Scope, float_type: Type, src: usize, lhs:
31453145 return Value.initPayload(val_payload);
31463146}
31473147
3148pub fn singlePtrType(self: *Module, scope: *Scope, src: usize, mutable: bool, elem_ty: Type) Allocator.Error!Type {
3149 const type_payload = try scope.arena().create(Type.Payload.Pointer);
3148pub fn simplePtrType(self: *Module, scope: *Scope, src: usize, elem_ty: Type, mutable: bool, size: std.builtin.TypeInfo.Pointer.Size) Allocator.Error!Type {
3149 // TODO stage1 type inference bug
3150 const T = Type.Tag;
3151
3152 const type_payload = try scope.arena().create(Type.Payload.PointerSimple);
31503153 type_payload.* = .{
3151 .base = .{ .tag = if (mutable) .single_mut_pointer else .single_const_pointer },
3154 .base = .{
3155 .tag = switch (size) {
3156 .One => if (mutable) .single_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,
3159 else => unreachable,
3160 },
3161 },
31523162 .pointee_type = elem_ty,
31533163 };
31543164 return Type.initPayload(&type_payload.base);
......@@ -3157,7 +3167,7 @@ pub fn singlePtrType(self: *Module, scope: *Scope, src: usize, mutable: bool, el
31573167pub fn optionalType(self: *Module, scope: *Scope, child_type: Type) Allocator.Error!Type {
31583168 return Type.initPayload(switch (child_type.tag()) {
31593169 .single_const_pointer => blk: {
3160 const payload = try scope.arena().create(Type.Payload.Pointer);
3170 const payload = try scope.arena().create(Type.Payload.PointerSimple);
31613171 payload.* = .{
31623172 .base = .{ .tag = .optional_single_const_pointer },
31633173 .pointee_type = child_type.elemType(),
......@@ -3165,7 +3175,7 @@ pub fn optionalType(self: *Module, scope: *Scope, child_type: Type) Allocator.Er
31653175 break :blk &payload.base;
31663176 },
31673177 .single_mut_pointer => blk: {
3168 const payload = try scope.arena().create(Type.Payload.Pointer);
3178 const payload = try scope.arena().create(Type.Payload.PointerSimple);
31693179 payload.* = .{
31703180 .base = .{ .tag = .optional_single_mut_pointer },
31713181 .pointee_type = child_type.elemType(),
src-self-hosted/astgen.zig+22-5
......@@ -577,6 +577,17 @@ fn ptrType(mod: *Module, scope: *Scope, node: *ast.Node.PtrType) InnerError!*zir
577577 .val = Value.initTag(.type_type),
578578 });
579579
580 const size: std.builtin.TypeInfo.Pointer.Size = switch (tree.token_ids[node.op_token]) {
581 .Asterisk, .AsteriskAsterisk => .One,
582 // TODO stage1 type inference bug
583 .LBracket => @as(std.builtin.TypeInfo.Pointer.Size, switch (tree.token_ids[node.op_token + 2]) {
584 .Identifier => .C,
585 .RBracket => .Many,
586 else => unreachable,
587 }),
588 else => unreachable,
589 };
590
580591 const simple = node.ptr_info.allowzero_token == null and
581592 node.ptr_info.align_info == null and
582593 node.ptr_info.volatile_token == null and
......@@ -584,13 +595,19 @@ fn ptrType(mod: *Module, scope: *Scope, node: *ast.Node.PtrType) InnerError!*zir
584595
585596 if (simple) {
586597 const child_type = try expr(mod, scope, .{ .ty = meta_type }, node.rhs);
587 return addZIRUnOp(mod, scope, src, if (node.ptr_info.const_token == null)
588 .single_mut_ptr_type
589 else
590 .single_const_ptr_type, child_type);
598 const mutable = node.ptr_info.const_token == null;
599 // TODO stage1 type inference bug
600 const T = zir.Inst.Tag;
601 return addZIRUnOp(mod, scope, src, switch (size) {
602 .One => if (mutable) T.single_mut_ptr_type else T.single_const_ptr_type,
603 .Many => if (mutable) T.many_mut_ptr_type else T.many_const_ptr_type,
604 .C => if (mutable) T.c_mut_ptr_type else T.c_const_ptr_type,
605 else => unreachable,
606 }, child_type);
591607 }
592608
593609 var kw_args: std.meta.fieldInfo(zir.Inst.PtrType, "kw_args").field_type = .{};
610 kw_args.size = size;
594611 kw_args.@"allowzero" = node.ptr_info.allowzero_token != null;
595612 if (node.ptr_info.align_info) |some| {
596613 kw_args.@"align" = try expr(mod, scope, .none, some.node);
......@@ -1271,7 +1288,7 @@ fn multilineStrLiteral(mod: *Module, scope: *Scope, node: *ast.Node.MultilineStr
12711288 i += 1;
12721289 }
12731290 const slice = tree.tokenSlice(line);
1274 mem.copy(u8, bytes[i..], slice[2..slice.len - 1]);
1291 mem.copy(u8, bytes[i..], slice[2 .. slice.len - 1]);
12751292 i += slice.len - 3;
12761293 }
12771294
src-self-hosted/codegen.zig+1-1
......@@ -2060,7 +2060,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
20602060 if (typed_value.val.isNull())
20612061 return MCValue{ .immediate = 0 };
20622062
2063 var buf: Type.Payload.Pointer = undefined;
2063 var buf: Type.Payload.PointerSimple = undefined;
20642064 return self.genTypedValue(src, .{
20652065 .ty = typed_value.ty.optionalChild(&buf),
20662066 .val = typed_value.val,
src-self-hosted/type.zig+159-22
......@@ -66,10 +66,15 @@ pub const Type = extern union {
6666 .function => return .Fn,
6767
6868 .array, .array_u8_sentinel_0, .array_u8, .array_sentinel => return .Array,
69 .single_const_pointer => return .Pointer,
70 .single_mut_pointer => return .Pointer,
71 .single_const_pointer_to_comptime_int => return .Pointer,
72 .const_slice_u8 => return .Pointer,
69 .single_const_pointer_to_comptime_int,
70 .const_slice_u8,
71 .single_const_pointer,
72 .single_mut_pointer,
73 .many_const_pointer,
74 .many_mut_pointer,
75 .c_const_pointer,
76 .c_mut_pointer,
77 => return .Pointer,
7378
7479 .optional,
7580 .optional_single_const_pointer,
......@@ -108,13 +113,17 @@ pub const Type = extern union {
108113 return @fieldParentPtr(T, "base", self.ptr_otherwise);
109114 }
110115
111 pub fn castPointer(self: Type) ?*Payload.Pointer {
116 pub fn castPointer(self: Type) ?*Payload.PointerSimple {
112117 return switch (self.tag()) {
113118 .single_const_pointer,
114119 .single_mut_pointer,
120 .many_const_pointer,
121 .many_mut_pointer,
122 .c_const_pointer,
123 .c_mut_pointer,
115124 .optional_single_const_pointer,
116125 .optional_single_mut_pointer,
117 => @fieldParentPtr(Payload.Pointer, "base", self.ptr_otherwise),
126 => @fieldParentPtr(Payload.PointerSimple, "base", self.ptr_otherwise),
118127 else => null,
119128 };
120129 }
......@@ -198,8 +207,8 @@ pub const Type = extern union {
198207 return true;
199208 },
200209 .Optional => {
201 var buf_a: Payload.Pointer = undefined;
202 var buf_b: Payload.Pointer = undefined;
210 var buf_a: Payload.PointerSimple = undefined;
211 var buf_b: Payload.PointerSimple = undefined;
203212 return a.optionalChild(&buf_a).eql(b.optionalChild(&buf_b));
204213 },
205214 .Float,
......@@ -263,7 +272,7 @@ pub const Type = extern union {
263272 }
264273 },
265274 .Optional => {
266 var buf: Payload.Pointer = undefined;
275 var buf: Payload.PointerSimple = undefined;
267276 std.hash.autoHash(&hasher, self.optionalChild(&buf).hash());
268277 },
269278 .Float,
......@@ -374,9 +383,13 @@ pub const Type = extern union {
374383 .optional => return self.copyPayloadSingleField(allocator, Payload.Optional, "child_type"),
375384 .single_const_pointer,
376385 .single_mut_pointer,
386 .many_const_pointer,
387 .many_mut_pointer,
388 .c_const_pointer,
389 .c_mut_pointer,
377390 .optional_single_mut_pointer,
378391 .optional_single_const_pointer,
379 => return self.copyPayloadSingleField(allocator, Payload.Pointer, "pointee_type"),
392 => return self.copyPayloadSingleField(allocator, Payload.PointerSimple, "pointee_type"),
380393 }
381394 }
382395
......@@ -482,17 +495,41 @@ pub const Type = extern union {
482495 continue;
483496 },
484497 .single_const_pointer => {
485 const payload = @fieldParentPtr(Payload.Pointer, "base", ty.ptr_otherwise);
498 const payload = @fieldParentPtr(Payload.PointerSimple, "base", ty.ptr_otherwise);
486499 try out_stream.writeAll("*const ");
487500 ty = payload.pointee_type;
488501 continue;
489502 },
490503 .single_mut_pointer => {
491 const payload = @fieldParentPtr(Payload.Pointer, "base", ty.ptr_otherwise);
504 const payload = @fieldParentPtr(Payload.PointerSimple, "base", ty.ptr_otherwise);
492505 try out_stream.writeAll("*");
493506 ty = payload.pointee_type;
494507 continue;
495508 },
509 .many_const_pointer => {
510 const payload = @fieldParentPtr(Payload.PointerSimple, "base", ty.ptr_otherwise);
511 try out_stream.writeAll("[*]const ");
512 ty = payload.pointee_type;
513 continue;
514 },
515 .many_mut_pointer => {
516 const payload = @fieldParentPtr(Payload.PointerSimple, "base", ty.ptr_otherwise);
517 try out_stream.writeAll("[*]");
518 ty = payload.pointee_type;
519 continue;
520 },
521 .c_const_pointer => {
522 const payload = @fieldParentPtr(Payload.PointerSimple, "base", ty.ptr_otherwise);
523 try out_stream.writeAll("[*c]const ");
524 ty = payload.pointee_type;
525 continue;
526 },
527 .c_mut_pointer => {
528 const payload = @fieldParentPtr(Payload.PointerSimple, "base", ty.ptr_otherwise);
529 try out_stream.writeAll("[*c]");
530 ty = payload.pointee_type;
531 continue;
532 },
496533 .int_signed => {
497534 const payload = @fieldParentPtr(Payload.IntSigned, "base", ty.ptr_otherwise);
498535 return out_stream.print("i{}", .{payload.bits});
......@@ -508,13 +545,13 @@ pub const Type = extern union {
508545 continue;
509546 },
510547 .optional_single_const_pointer => {
511 const payload = @fieldParentPtr(Payload.Pointer, "base", ty.ptr_otherwise);
548 const payload = @fieldParentPtr(Payload.PointerSimple, "base", ty.ptr_otherwise);
512549 try out_stream.writeAll("?*const ");
513550 ty = payload.pointee_type;
514551 continue;
515552 },
516553 .optional_single_mut_pointer => {
517 const payload = @fieldParentPtr(Payload.Pointer, "base", ty.ptr_otherwise);
554 const payload = @fieldParentPtr(Payload.PointerSimple, "base", ty.ptr_otherwise);
518555 try out_stream.writeAll("?*");
519556 ty = payload.pointee_type;
520557 continue;
......@@ -619,6 +656,10 @@ pub const Type = extern union {
619656 .array_sentinel => self.elemType().hasCodeGenBits(),
620657 .single_const_pointer => self.elemType().hasCodeGenBits(),
621658 .single_mut_pointer => self.elemType().hasCodeGenBits(),
659 .many_const_pointer => self.elemType().hasCodeGenBits(),
660 .many_mut_pointer => self.elemType().hasCodeGenBits(),
661 .c_const_pointer => self.elemType().hasCodeGenBits(),
662 .c_mut_pointer => self.elemType().hasCodeGenBits(),
622663 .int_signed => self.cast(Payload.IntSigned).?.bits == 0,
623664 .int_unsigned => self.cast(Payload.IntUnsigned).?.bits == 0,
624665
......@@ -669,6 +710,10 @@ pub const Type = extern union {
669710 .const_slice_u8,
670711 .single_const_pointer,
671712 .single_mut_pointer,
713 .many_const_pointer,
714 .many_mut_pointer,
715 .c_const_pointer,
716 .c_mut_pointer,
672717 .optional_single_const_pointer,
673718 .optional_single_mut_pointer,
674719 => return @divExact(target.cpu.arch.ptrBitWidth(), 8),
......@@ -704,7 +749,7 @@ pub const Type = extern union {
704749 },
705750
706751 .optional => {
707 var buf: Payload.Pointer = undefined;
752 var buf: Payload.PointerSimple = undefined;
708753 const child_type = self.optionalChild(&buf);
709754 if (!child_type.hasCodeGenBits()) return 1;
710755
......@@ -772,6 +817,10 @@ pub const Type = extern union {
772817 .const_slice_u8,
773818 .single_const_pointer,
774819 .single_mut_pointer,
820 .many_const_pointer,
821 .many_mut_pointer,
822 .c_const_pointer,
823 .c_mut_pointer,
775824 .optional_single_const_pointer,
776825 .optional_single_mut_pointer,
777826 => return @divExact(target.cpu.arch.ptrBitWidth(), 8),
......@@ -805,7 +854,7 @@ pub const Type = extern union {
805854 },
806855
807856 .optional => {
808 var buf: Payload.Pointer = undefined;
857 var buf: Payload.PointerSimple = undefined;
809858 const child_type = self.optionalChild(&buf);
810859 if (!child_type.hasCodeGenBits()) return 1;
811860
......@@ -872,6 +921,10 @@ pub const Type = extern union {
872921 .optional_single_mut_pointer,
873922 .optional_single_const_pointer,
874923 .enum_literal,
924 .many_const_pointer,
925 .many_mut_pointer,
926 .c_const_pointer,
927 .c_mut_pointer,
875928 => false,
876929
877930 .single_const_pointer,
......@@ -922,6 +975,10 @@ pub const Type = extern union {
922975 .array_u8_sentinel_0,
923976 .single_const_pointer,
924977 .single_mut_pointer,
978 .many_const_pointer,
979 .many_mut_pointer,
980 .c_const_pointer,
981 .c_mut_pointer,
925982 .single_const_pointer_to_comptime_int,
926983 .fn_noreturn_no_args,
927984 .fn_void_no_args,
......@@ -987,6 +1044,8 @@ pub const Type = extern union {
9871044 .int_unsigned,
9881045 .int_signed,
9891046 .single_mut_pointer,
1047 .many_mut_pointer,
1048 .c_mut_pointer,
9901049 .optional,
9911050 .optional_single_mut_pointer,
9921051 .optional_single_const_pointer,
......@@ -994,6 +1053,8 @@ pub const Type = extern union {
9941053 => false,
9951054
9961055 .single_const_pointer,
1056 .many_const_pointer,
1057 .c_const_pointer,
9971058 .single_const_pointer_to_comptime_int,
9981059 .const_slice_u8,
9991060 => true,
......@@ -1048,6 +1109,10 @@ pub const Type = extern union {
10481109 .int_signed,
10491110 .single_mut_pointer,
10501111 .single_const_pointer,
1112 .many_const_pointer,
1113 .many_mut_pointer,
1114 .c_const_pointer,
1115 .c_mut_pointer,
10511116 .single_const_pointer_to_comptime_int,
10521117 .const_slice_u8,
10531118 .optional,
......@@ -1063,7 +1128,7 @@ pub const Type = extern union {
10631128 switch (self.tag()) {
10641129 .optional_single_const_pointer, .optional_single_mut_pointer => return true,
10651130 .optional => {
1066 var buf: Payload.Pointer = undefined;
1131 var buf: Payload.PointerSimple = undefined;
10671132 const child_type = self.optionalChild(&buf);
10681133 // optionals of zero sized pointers behave like bools
10691134 if (!child_type.hasCodeGenBits()) return false;
......@@ -1101,7 +1166,7 @@ pub const Type = extern union {
11011166 => return false,
11021167
11031168 .Optional => {
1104 var buf: Payload.Pointer = undefined;
1169 var buf: Payload.PointerSimple = undefined;
11051170 return ty.optionalChild(&buf).isValidVarType(is_extern);
11061171 },
11071172 .Pointer, .Array => ty = ty.elemType(),
......@@ -1166,13 +1231,17 @@ pub const Type = extern union {
11661231 .array_sentinel => self.cast(Payload.ArraySentinel).?.elem_type,
11671232 .single_const_pointer => self.castPointer().?.pointee_type,
11681233 .single_mut_pointer => self.castPointer().?.pointee_type,
1234 .many_const_pointer => self.castPointer().?.pointee_type,
1235 .many_mut_pointer => self.castPointer().?.pointee_type,
1236 .c_const_pointer => self.castPointer().?.pointee_type,
1237 .c_mut_pointer => self.castPointer().?.pointee_type,
11691238 .array_u8, .array_u8_sentinel_0, .const_slice_u8 => Type.initTag(.u8),
11701239 .single_const_pointer_to_comptime_int => Type.initTag(.comptime_int),
11711240 };
11721241 }
11731242
11741243 /// Asserts that the type is an optional.
1175 pub fn optionalChild(self: Type, buf: *Payload.Pointer) Type {
1244 pub fn optionalChild(self: Type, buf: *Payload.PointerSimple) Type {
11761245 return switch (self.tag()) {
11771246 .optional => self.cast(Payload.Optional).?.child_type,
11781247 .optional_single_mut_pointer => {
......@@ -1199,7 +1268,7 @@ pub const Type = extern union {
11991268 return switch (self.tag()) {
12001269 .optional => self.cast(Payload.Optional).?.child_type,
12011270 .optional_single_mut_pointer, .optional_single_const_pointer => {
1202 const payload = try allocator.create(Payload.Pointer);
1271 const payload = try allocator.create(Payload.PointerSimple);
12031272 payload.* = .{
12041273 .base = .{
12051274 .tag = if (self.tag() == .optional_single_const_pointer)
......@@ -1258,6 +1327,10 @@ pub const Type = extern union {
12581327 .function,
12591328 .single_const_pointer,
12601329 .single_mut_pointer,
1330 .many_const_pointer,
1331 .many_mut_pointer,
1332 .c_const_pointer,
1333 .c_mut_pointer,
12611334 .single_const_pointer_to_comptime_int,
12621335 .const_slice_u8,
12631336 .int_unsigned,
......@@ -1318,6 +1391,10 @@ pub const Type = extern union {
13181391 .function,
13191392 .single_const_pointer,
13201393 .single_mut_pointer,
1394 .many_const_pointer,
1395 .many_mut_pointer,
1396 .c_const_pointer,
1397 .c_mut_pointer,
13211398 .single_const_pointer_to_comptime_int,
13221399 .const_slice_u8,
13231400 .int_unsigned,
......@@ -1368,6 +1445,10 @@ pub const Type = extern union {
13681445 .array_u8_sentinel_0,
13691446 .single_const_pointer,
13701447 .single_mut_pointer,
1448 .many_const_pointer,
1449 .many_mut_pointer,
1450 .c_const_pointer,
1451 .c_mut_pointer,
13711452 .single_const_pointer_to_comptime_int,
13721453 .const_slice_u8,
13731454 .int_unsigned,
......@@ -1429,6 +1510,10 @@ pub const Type = extern union {
14291510 .array_u8_sentinel_0,
14301511 .single_const_pointer,
14311512 .single_mut_pointer,
1513 .many_const_pointer,
1514 .many_mut_pointer,
1515 .c_const_pointer,
1516 .c_mut_pointer,
14321517 .single_const_pointer_to_comptime_int,
14331518 .const_slice_u8,
14341519 .int_signed,
......@@ -1490,6 +1575,10 @@ pub const Type = extern union {
14901575 .array_u8_sentinel_0,
14911576 .single_const_pointer,
14921577 .single_mut_pointer,
1578 .many_const_pointer,
1579 .many_mut_pointer,
1580 .c_const_pointer,
1581 .c_mut_pointer,
14931582 .single_const_pointer_to_comptime_int,
14941583 .const_slice_u8,
14951584 .optional,
......@@ -1549,6 +1638,10 @@ pub const Type = extern union {
15491638 .array_u8_sentinel_0,
15501639 .single_const_pointer,
15511640 .single_mut_pointer,
1641 .many_const_pointer,
1642 .many_mut_pointer,
1643 .c_const_pointer,
1644 .c_mut_pointer,
15521645 .single_const_pointer_to_comptime_int,
15531646 .const_slice_u8,
15541647 .int_unsigned,
......@@ -1637,6 +1730,10 @@ pub const Type = extern union {
16371730 .array_u8_sentinel_0,
16381731 .single_const_pointer,
16391732 .single_mut_pointer,
1733 .many_const_pointer,
1734 .many_mut_pointer,
1735 .c_const_pointer,
1736 .c_mut_pointer,
16401737 .single_const_pointer_to_comptime_int,
16411738 .const_slice_u8,
16421739 .u8,
......@@ -1701,6 +1798,10 @@ pub const Type = extern union {
17011798 .array_u8_sentinel_0,
17021799 .single_const_pointer,
17031800 .single_mut_pointer,
1801 .many_const_pointer,
1802 .many_mut_pointer,
1803 .c_const_pointer,
1804 .c_mut_pointer,
17041805 .single_const_pointer_to_comptime_int,
17051806 .const_slice_u8,
17061807 .u8,
......@@ -1764,6 +1865,10 @@ pub const Type = extern union {
17641865 .array_u8_sentinel_0,
17651866 .single_const_pointer,
17661867 .single_mut_pointer,
1868 .many_const_pointer,
1869 .many_mut_pointer,
1870 .c_const_pointer,
1871 .c_mut_pointer,
17671872 .single_const_pointer_to_comptime_int,
17681873 .const_slice_u8,
17691874 .u8,
......@@ -1827,6 +1932,10 @@ pub const Type = extern union {
18271932 .array_u8_sentinel_0,
18281933 .single_const_pointer,
18291934 .single_mut_pointer,
1935 .many_const_pointer,
1936 .many_mut_pointer,
1937 .c_const_pointer,
1938 .c_mut_pointer,
18301939 .single_const_pointer_to_comptime_int,
18311940 .const_slice_u8,
18321941 .u8,
......@@ -1887,6 +1996,10 @@ pub const Type = extern union {
18871996 .array_u8_sentinel_0,
18881997 .single_const_pointer,
18891998 .single_mut_pointer,
1999 .many_const_pointer,
2000 .many_mut_pointer,
2001 .c_const_pointer,
2002 .c_mut_pointer,
18902003 .single_const_pointer_to_comptime_int,
18912004 .const_slice_u8,
18922005 .u8,
......@@ -1947,6 +2060,10 @@ pub const Type = extern union {
19472060 .array_u8_sentinel_0,
19482061 .single_const_pointer,
19492062 .single_mut_pointer,
2063 .many_const_pointer,
2064 .many_mut_pointer,
2065 .c_const_pointer,
2066 .c_mut_pointer,
19502067 .single_const_pointer_to_comptime_int,
19512068 .const_slice_u8,
19522069 .u8,
......@@ -2027,6 +2144,10 @@ pub const Type = extern union {
20272144 .array_u8_sentinel_0,
20282145 .single_const_pointer,
20292146 .single_mut_pointer,
2147 .many_const_pointer,
2148 .many_mut_pointer,
2149 .c_const_pointer,
2150 .c_mut_pointer,
20302151 .single_const_pointer_to_comptime_int,
20312152 .const_slice_u8,
20322153 .optional,
......@@ -2109,7 +2230,13 @@ pub const Type = extern union {
21092230 ty = ty.elemType();
21102231 continue;
21112232 },
2112 .single_const_pointer, .single_mut_pointer => {
2233 .many_const_pointer,
2234 .many_mut_pointer,
2235 .c_const_pointer,
2236 .c_mut_pointer,
2237 .single_const_pointer,
2238 .single_mut_pointer,
2239 => {
21132240 const ptr = ty.castPointer().?;
21142241 ty = ptr.pointee_type;
21152242 continue;
......@@ -2167,11 +2294,17 @@ pub const Type = extern union {
21672294 .array_u8_sentinel_0,
21682295 .single_const_pointer,
21692296 .single_mut_pointer,
2297 .many_const_pointer,
2298 .many_mut_pointer,
21702299 .optional,
21712300 .optional_single_mut_pointer,
21722301 .optional_single_const_pointer,
21732302 .enum_literal,
21742303 => return false,
2304
2305 .c_const_pointer,
2306 .c_mut_pointer,
2307 => return true,
21752308 };
21762309 }
21772310
......@@ -2231,6 +2364,10 @@ pub const Type = extern union {
22312364 array_sentinel,
22322365 single_const_pointer,
22332366 single_mut_pointer,
2367 many_const_pointer,
2368 many_mut_pointer,
2369 c_const_pointer,
2370 c_mut_pointer,
22342371 int_signed,
22352372 int_unsigned,
22362373 function,
......@@ -2272,7 +2409,7 @@ pub const Type = extern union {
22722409 elem_type: Type,
22732410 };
22742411
2275 pub const Pointer = struct {
2412 pub const PointerSimple = struct {
22762413 base: Payload,
22772414
22782415 pointee_type: Type,
src-self-hosted/zir.zig+18-1
......@@ -198,6 +198,14 @@ pub const Inst = struct {
198198 single_const_ptr_type,
199199 /// Create a mutable pointer type based on the element type. `*T`
200200 single_mut_ptr_type,
201 /// Create a const pointer type based on the element type. `[*]const T`
202 many_const_ptr_type,
203 /// Create a mutable pointer type based on the element type. `[*]T`
204 many_mut_ptr_type,
205 /// Create a const pointer type based on the element type. `[*c]const T`
206 c_const_ptr_type,
207 /// Create a mutable pointer type based on the element type. `[*c]T`
208 c_mut_ptr_type,
201209 /// Create a pointer type with attributes
202210 ptr_type,
203211 /// Write a value to a pointer. For loading, see `deref`.
......@@ -262,6 +270,10 @@ pub const Inst = struct {
262270 .typeof,
263271 .single_const_ptr_type,
264272 .single_mut_ptr_type,
273 .many_const_ptr_type,
274 .many_mut_ptr_type,
275 .c_const_ptr_type,
276 .c_mut_ptr_type,
265277 .optional_type,
266278 .unwrap_optional_safe,
267279 .unwrap_optional_unsafe,
......@@ -400,6 +412,10 @@ pub const Inst = struct {
400412 .shr,
401413 .single_const_ptr_type,
402414 .single_mut_ptr_type,
415 .many_const_ptr_type,
416 .many_mut_ptr_type,
417 .c_const_ptr_type,
418 .c_mut_ptr_type,
403419 .store,
404420 .str,
405421 .sub,
......@@ -859,6 +875,7 @@ pub const Inst = struct {
859875 @"const": bool = true,
860876 @"volatile": bool = false,
861877 sentinel: ?*Inst = null,
878 size: std.builtin.TypeInfo.Pointer.Size = .One,
862879 },
863880 };
864881
......@@ -2443,7 +2460,7 @@ const EmitZIR = struct {
24432460 }
24442461 },
24452462 .Optional => {
2446 var buf: Type.Payload.Pointer = undefined;
2463 var buf: Type.Payload.PointerSimple = undefined;
24472464 const inst = try self.arena.allocator.create(Inst.UnOp);
24482465 inst.* = .{
24492466 .base = .{
src-self-hosted/zir_sema.zig+12-14
......@@ -51,8 +51,12 @@ pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!
5151 .ref => return analyzeInstRef(mod, scope, old_inst.castTag(.ref).?),
5252 .ret_ptr => return analyzeInstRetPtr(mod, scope, old_inst.castTag(.ret_ptr).?),
5353 .ret_type => return analyzeInstRetType(mod, scope, old_inst.castTag(.ret_type).?),
54 .single_const_ptr_type => return analyzeInstSingleConstPtrType(mod, scope, old_inst.castTag(.single_const_ptr_type).?),
55 .single_mut_ptr_type => return analyzeInstSingleMutPtrType(mod, scope, old_inst.castTag(.single_mut_ptr_type).?),
54 .single_const_ptr_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.single_const_ptr_type).?, false, .One),
55 .single_mut_ptr_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.single_mut_ptr_type).?, true, .One),
56 .many_const_ptr_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.many_const_ptr_type).?, false, .Many),
57 .many_mut_ptr_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.many_mut_ptr_type).?, true, .Many),
58 .c_const_ptr_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.c_const_ptr_type).?, false, .C),
59 .c_mut_ptr_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.c_mut_ptr_type).?, true, .C),
5660 .ptr_type => return analyzeInstPtrType(mod, scope, old_inst.castTag(.ptr_type).?),
5761 .store => return analyzeInstStore(mod, scope, old_inst.castTag(.store).?),
5862 .str => return analyzeInstStr(mod, scope, old_inst.castTag(.str).?),
......@@ -324,7 +328,7 @@ fn analyzeInstRetPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.NoOp) InnerErr
324328
325329fn analyzeInstRef(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
326330 const operand = try resolveInst(mod, scope, inst.positionals.operand);
327 const ptr_type = try mod.singlePtrType(scope, inst.base.src, false, operand.ty);
331 const ptr_type = try mod.simplePtrType(scope, inst.base.src, operand.ty, false, .One);
328332
329333 if (operand.value()) |val| {
330334 const ref_payload = try scope.arena().create(Value.Payload.RefVal);
......@@ -369,7 +373,7 @@ fn analyzeInstAlloc(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerErro
369373 if (!var_type.isValidVarType(false)) {
370374 return mod.fail(scope, inst.base.src, "variable of type '{}' must be const or comptime", .{var_type});
371375 }
372 const ptr_type = try mod.singlePtrType(scope, inst.base.src, true, var_type);
376 const ptr_type = try mod.simplePtrType(scope, inst.base.src, var_type, true, .One);
373377 const b = try mod.requireRuntimeBlock(scope, inst.base.src);
374378 return mod.addNoOp(b, inst.base.src, ptr_type, .alloc);
375379}
......@@ -723,7 +727,7 @@ fn analyzeInstUnwrapOptional(mod: *Module, scope: *Scope, unwrap: *zir.Inst.UnOp
723727 }
724728
725729 const child_type = try operand.ty.elemType().optionalChildAlloc(scope.arena());
726 const child_pointer = try mod.singlePtrType(scope, unwrap.base.src, operand.ty.isConstPtr(), child_type);
730 const child_pointer = try mod.simplePtrType(scope, unwrap.base.src, child_type, operand.ty.isConstPtr(), .One);
727731
728732 if (operand.value()) |val| {
729733 if (val.isNull()) {
......@@ -940,7 +944,7 @@ fn analyzeInstElemPtr(mod: *Module, scope: *Scope, inst: *zir.Inst.ElemPtr) Inne
940944 // required a larger index.
941945 const elem_ptr = try array_ptr_val.elemPtr(scope.arena(), @intCast(usize, index_u64));
942946
943 const type_payload = try scope.arena().create(Type.Payload.Pointer);
947 const type_payload = try scope.arena().create(Type.Payload.PointerSimple);
944948 type_payload.* = .{
945949 .base = .{ .tag = .single_const_pointer },
946950 .pointee_type = array_ptr.ty.elemType().elemType(),
......@@ -1311,15 +1315,9 @@ fn analyzeDeclVal(mod: *Module, scope: *Scope, inst: *zir.Inst.DeclVal) InnerErr
13111315 return decl;
13121316}
13131317
1314fn analyzeInstSingleConstPtrType(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
1318fn analyzeInstSimplePtrType(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp, mutable: bool, size: std.builtin.TypeInfo.Pointer.Size) InnerError!*Inst {
13151319 const elem_type = try resolveType(mod, scope, inst.positionals.operand);
1316 const ty = try mod.singlePtrType(scope, inst.base.src, false, elem_type);
1317 return mod.constType(scope, inst.base.src, ty);
1318}
1319
1320fn analyzeInstSingleMutPtrType(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
1321 const elem_type = try resolveType(mod, scope, inst.positionals.operand);
1322 const ty = try mod.singlePtrType(scope, inst.base.src, true, elem_type);
1320 const ty = try mod.simplePtrType(scope, inst.base.src, elem_type, mutable, size);
13231321 return mod.constType(scope, inst.base.src, ty);
13241322}
13251323