authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-20 16:37:28+03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-20 16:42:48-07:00
logd312d64c9aa058293d69b2bf97c51f4caa7a2d9d
tree3c8a6a4ec1d691157b3098d3dcd3be1027d5adf2
parent95682484503f031efefb2c085cdd4d0b64cf8795

stage2: slice types


5 files changed, 127 insertions(+), 47 deletions(-)

src-self-hosted/Module.zig+4-1
......@@ -3146,6 +3146,9 @@ pub fn floatSub(self: *Module, scope: *Scope, float_type: Type, src: usize, lhs:
31463146}
31473147
31483148pub fn simplePtrType(self: *Module, scope: *Scope, src: usize, elem_ty: Type, mutable: bool, size: std.builtin.TypeInfo.Pointer.Size) Allocator.Error!Type {
3149 if (size == .Slice and elem_ty.eql(Type.initTag(.u8))) {
3150 return Type.initTag(.const_slice_u8);
3151 }
31493152 // TODO stage1 type inference bug
31503153 const T = Type.Tag;
31513154
......@@ -3156,7 +3159,7 @@ pub fn simplePtrType(self: *Module, scope: *Scope, src: usize, elem_ty: Type, mu
31563159 .One => if (mutable) T.single_mut_pointer else T.single_const_pointer,
31573160 .Many => if (mutable) T.many_mut_pointer else T.many_const_pointer,
31583161 .C => if (mutable) T.c_mut_pointer else T.c_const_pointer,
3159 else => unreachable,
3162 .Slice => if (mutable) T.mut_slice else T.const_slice,
31603163 },
31613164 },
31623165 .pointee_type = elem_ty,
src-self-hosted/astgen.zig+29-21
......@@ -262,6 +262,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr
262262 .EnumLiteral => return rlWrap(mod, scope, rl, try enumLiteral(mod, scope, node.castTag(.EnumLiteral).?)),
263263 .MultilineStringLiteral => return rlWrap(mod, scope, rl, try multilineStrLiteral(mod, scope, node.castTag(.MultilineStringLiteral).?)),
264264 .CharLiteral => return rlWrap(mod, scope, rl, try charLiteral(mod, scope, node.castTag(.CharLiteral).?)),
265 .SliceType => return rlWrap(mod, scope, rl, try sliceType(mod, scope, node.castTag(.SliceType).?)),
265266
266267 .Defer => return mod.failNode(scope, node, "TODO implement astgen.expr for .Defer", .{}),
267268 .Catch => return mod.failNode(scope, node, "TODO implement astgen.expr for .Catch", .{}),
......@@ -275,7 +276,6 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr
275276 .NegationWrap => return mod.failNode(scope, node, "TODO implement astgen.expr for .NegationWrap", .{}),
276277 .Resume => return mod.failNode(scope, node, "TODO implement astgen.expr for .Resume", .{}),
277278 .Try => return mod.failNode(scope, node, "TODO implement astgen.expr for .Try", .{}),
278 .SliceType => return mod.failNode(scope, node, "TODO implement astgen.expr for .SliceType", .{}),
279279 .Slice => return mod.failNode(scope, node, "TODO implement astgen.expr for .Slice", .{}),
280280 .ArrayAccess => return mod.failNode(scope, node, "TODO implement astgen.expr for .ArrayAccess", .{}),
281281 .ArrayInitializer => return mod.failNode(scope, node, "TODO implement astgen.expr for .ArrayInitializer", .{}),
......@@ -569,15 +569,16 @@ fn optionalType(mod: *Module, scope: *Scope, node: *ast.Node.SimplePrefixOp) Inn
569569 return addZIRUnOp(mod, scope, src, .optional_type, operand);
570570}
571571
572fn ptrType(mod: *Module, scope: *Scope, node: *ast.Node.PtrType) InnerError!*zir.Inst {
572fn sliceType(mod: *Module, scope: *Scope, node: *ast.Node.SliceType) InnerError!*zir.Inst {
573573 const tree = scope.tree();
574574 const src = tree.token_locs[node.op_token].start;
575 const meta_type = try addZIRInstConst(mod, scope, src, .{
576 .ty = Type.initTag(.type),
577 .val = Value.initTag(.type_type),
578 });
575 return ptrSliceType(mod, scope, src, &node.ptr_info, node.rhs, .Slice);
576}
579577
580 const size: std.builtin.TypeInfo.Pointer.Size = switch (tree.token_ids[node.op_token]) {
578fn ptrType(mod: *Module, scope: *Scope, node: *ast.Node.PtrType) InnerError!*zir.Inst {
579 const tree = scope.tree();
580 const src = tree.token_locs[node.op_token].start;
581 return ptrSliceType(mod, scope, src, &node.ptr_info, node.rhs, switch (tree.token_ids[node.op_token]) {
581582 .Asterisk, .AsteriskAsterisk => .One,
582583 // TODO stage1 type inference bug
583584 .LBracket => @as(std.builtin.TypeInfo.Pointer.Size, switch (tree.token_ids[node.op_token + 2]) {
......@@ -585,43 +586,50 @@ fn ptrType(mod: *Module, scope: *Scope, node: *ast.Node.PtrType) InnerError!*zir
585586 else => .Many,
586587 }),
587588 else => unreachable,
588 };
589 });
590}
589591
590 const simple = node.ptr_info.allowzero_token == null and
591 node.ptr_info.align_info == null and
592 node.ptr_info.volatile_token == null and
593 node.ptr_info.sentinel == null;
592fn ptrSliceType(mod: *Module, scope: *Scope, src: usize, ptr_info: *ast.PtrInfo, rhs: *ast.Node, size: std.builtin.TypeInfo.Pointer.Size) InnerError!*zir.Inst {
593 const meta_type = try addZIRInstConst(mod, scope, src, .{
594 .ty = Type.initTag(.type),
595 .val = Value.initTag(.type_type),
596 });
597
598 const simple = ptr_info.allowzero_token == null and
599 ptr_info.align_info == null and
600 ptr_info.volatile_token == null and
601 ptr_info.sentinel == null;
594602
595603 if (simple) {
596 const child_type = try expr(mod, scope, .{ .ty = meta_type }, node.rhs);
597 const mutable = node.ptr_info.const_token == null;
604 const child_type = try expr(mod, scope, .{ .ty = meta_type }, rhs);
605 const mutable = ptr_info.const_token == null;
598606 // TODO stage1 type inference bug
599607 const T = zir.Inst.Tag;
600608 return addZIRUnOp(mod, scope, src, switch (size) {
601609 .One => if (mutable) T.single_mut_ptr_type else T.single_const_ptr_type,
602610 .Many => if (mutable) T.many_mut_ptr_type else T.many_const_ptr_type,
603611 .C => if (mutable) T.c_mut_ptr_type else T.c_const_ptr_type,
604 else => unreachable,
612 .Slice => if (mutable) T.mut_slice_type else T.mut_slice_type,
605613 }, child_type);
606614 }
607615
608616 var kw_args: std.meta.fieldInfo(zir.Inst.PtrType, "kw_args").field_type = .{};
609617 kw_args.size = size;
610 kw_args.@"allowzero" = node.ptr_info.allowzero_token != null;
611 if (node.ptr_info.align_info) |some| {
618 kw_args.@"allowzero" = ptr_info.allowzero_token != null;
619 if (ptr_info.align_info) |some| {
612620 kw_args.@"align" = try expr(mod, scope, .none, some.node);
613621 if (some.bit_range) |bit_range| {
614622 kw_args.align_bit_start = try expr(mod, scope, .none, bit_range.start);
615623 kw_args.align_bit_end = try expr(mod, scope, .none, bit_range.end);
616624 }
617625 }
618 kw_args.mutable = node.ptr_info.const_token == null;
619 kw_args.@"volatile" = node.ptr_info.volatile_token != null;
620 if (node.ptr_info.sentinel) |some| {
626 kw_args.mutable = ptr_info.const_token == null;
627 kw_args.@"volatile" = ptr_info.volatile_token != null;
628 if (ptr_info.sentinel) |some| {
621629 kw_args.sentinel = try expr(mod, scope, .none, some);
622630 }
623631
624 const child_type = try expr(mod, scope, .{ .ty = meta_type }, node.rhs);
632 const child_type = try expr(mod, scope, .{ .ty = meta_type }, rhs);
625633 if (kw_args.sentinel) |some| {
626634 kw_args.sentinel = try addZIRBinOp(mod, scope, some.src, .as, child_type, some);
627635 }
src-self-hosted/type.zig+78-19
......@@ -74,6 +74,8 @@ pub const Type = extern union {
7474 .many_mut_pointer,
7575 .c_const_pointer,
7676 .c_mut_pointer,
77 .const_slice,
78 .mut_slice,
7779 .pointer,
7880 => return .Pointer,
7981
......@@ -122,6 +124,8 @@ pub const Type = extern union {
122124 .many_mut_pointer,
123125 .c_const_pointer,
124126 .c_mut_pointer,
127 .const_slice,
128 .mut_slice,
125129 .optional_single_const_pointer,
126130 .optional_single_mut_pointer,
127131 => @fieldParentPtr(Payload.PointerSimple, "base", self.ptr_otherwise),
......@@ -388,6 +392,8 @@ pub const Type = extern union {
388392 .many_mut_pointer,
389393 .c_const_pointer,
390394 .c_mut_pointer,
395 .const_slice,
396 .mut_slice,
391397 .optional_single_mut_pointer,
392398 .optional_single_const_pointer,
393399 => return self.copyPayloadSingleField(allocator, Payload.PointerSimple, "pointee_type"),
......@@ -550,6 +556,18 @@ pub const Type = extern union {
550556 ty = payload.pointee_type;
551557 continue;
552558 },
559 .const_slice => {
560 const payload = @fieldParentPtr(Payload.PointerSimple, "base", ty.ptr_otherwise);
561 try out_stream.writeAll("[]const ");
562 ty = payload.pointee_type;
563 continue;
564 },
565 .mut_slice => {
566 const payload = @fieldParentPtr(Payload.PointerSimple, "base", ty.ptr_otherwise);
567 try out_stream.writeAll("[]");
568 ty = payload.pointee_type;
569 continue;
570 },
553571 .int_signed => {
554572 const payload = @fieldParentPtr(Payload.IntSigned, "base", ty.ptr_otherwise);
555573 return out_stream.print("i{}", .{payload.bits});
......@@ -701,14 +719,7 @@ pub const Type = extern union {
701719 // TODO lazy types
702720 .array => self.elemType().hasCodeGenBits() and self.arrayLen() != 0,
703721 .array_u8 => self.arrayLen() != 0,
704 .array_sentinel => self.elemType().hasCodeGenBits(),
705 .single_const_pointer => self.elemType().hasCodeGenBits(),
706 .single_mut_pointer => self.elemType().hasCodeGenBits(),
707 .many_const_pointer => self.elemType().hasCodeGenBits(),
708 .many_mut_pointer => self.elemType().hasCodeGenBits(),
709 .c_const_pointer => self.elemType().hasCodeGenBits(),
710 .c_mut_pointer => self.elemType().hasCodeGenBits(),
711 .pointer => self.elemType().hasCodeGenBits(),
722 .array_sentinel, .single_const_pointer, .single_mut_pointer, .many_const_pointer, .many_mut_pointer, .c_const_pointer, .c_mut_pointer, .const_slice, .mut_slice, .pointer => self.elemType().hasCodeGenBits(),
712723 .int_signed => self.cast(Payload.IntSigned).?.bits == 0,
713724 .int_unsigned => self.cast(Payload.IntUnsigned).?.bits == 0,
714725
......@@ -763,6 +774,8 @@ pub const Type = extern union {
763774 .many_mut_pointer,
764775 .c_const_pointer,
765776 .c_mut_pointer,
777 .const_slice,
778 .mut_slice,
766779 .optional_single_const_pointer,
767780 .optional_single_mut_pointer,
768781 => return @divExact(target.cpu.arch.ptrBitWidth(), 8),
......@@ -868,10 +881,12 @@ pub const Type = extern union {
868881 .i32, .u32 => return 4,
869882 .i64, .u64 => return 8,
870883
871 .isize,
872 .usize
873 => return @divExact(target.cpu.arch.ptrBitWidth(), 8),
874 .const_slice_u8 => return @divExact(target.cpu.arch.ptrBitWidth(), 8) * 2,
884 .isize, .usize => return @divExact(target.cpu.arch.ptrBitWidth(), 8),
885
886 .const_slice,
887 .mut_slice,
888 .const_slice_u8,
889 => return @divExact(target.cpu.arch.ptrBitWidth(), 8) * 2,
875890
876891 .optional_single_const_pointer,
877892 .optional_single_mut_pointer,
......@@ -992,6 +1007,8 @@ pub const Type = extern union {
9921007 .many_mut_pointer,
9931008 .c_const_pointer,
9941009 .c_mut_pointer,
1010 .const_slice,
1011 .mut_slice,
9951012 => false,
9961013
9971014 .single_const_pointer,
......@@ -1062,7 +1079,10 @@ pub const Type = extern union {
10621079 .enum_literal,
10631080 => false,
10641081
1065 .const_slice_u8 => true,
1082 .const_slice,
1083 .mut_slice,
1084 .const_slice_u8,
1085 => true,
10661086
10671087 .pointer => self.cast(Payload.Pointer).?.size == .Slice,
10681088 };
......@@ -1121,6 +1141,7 @@ pub const Type = extern union {
11211141 .optional_single_mut_pointer,
11221142 .optional_single_const_pointer,
11231143 .enum_literal,
1144 .mut_slice,
11241145 => false,
11251146
11261147 .single_const_pointer,
......@@ -1128,6 +1149,7 @@ pub const Type = extern union {
11281149 .c_const_pointer,
11291150 .single_const_pointer_to_comptime_int,
11301151 .const_slice_u8,
1152 .const_slice,
11311153 => true,
11321154
11331155 .pointer => !self.cast(Payload.Pointer).?.mutable,
......@@ -1186,6 +1208,8 @@ pub const Type = extern union {
11861208 .many_mut_pointer,
11871209 .c_const_pointer,
11881210 .c_mut_pointer,
1211 .const_slice,
1212 .mut_slice,
11891213 .single_const_pointer_to_comptime_int,
11901214 .const_slice_u8,
11911215 .optional,
......@@ -1307,12 +1331,15 @@ pub const Type = extern union {
13071331
13081332 .array => self.cast(Payload.Array).?.elem_type,
13091333 .array_sentinel => self.cast(Payload.ArraySentinel).?.elem_type,
1310 .single_const_pointer => self.castPointer().?.pointee_type,
1311 .single_mut_pointer => self.castPointer().?.pointee_type,
1312 .many_const_pointer => self.castPointer().?.pointee_type,
1313 .many_mut_pointer => self.castPointer().?.pointee_type,
1314 .c_const_pointer => self.castPointer().?.pointee_type,
1315 .c_mut_pointer => self.castPointer().?.pointee_type,
1334 .single_const_pointer,
1335 .single_mut_pointer,
1336 .many_const_pointer,
1337 .many_mut_pointer,
1338 .c_const_pointer,
1339 .c_mut_pointer,
1340 .const_slice,
1341 .mut_slice,
1342 => self.castPointer().?.pointee_type,
13161343 .array_u8, .array_u8_sentinel_0, .const_slice_u8 => Type.initTag(.u8),
13171344 .single_const_pointer_to_comptime_int => Type.initTag(.comptime_int),
13181345 .pointer => self.cast(Payload.Pointer).?.pointee_type,
......@@ -1411,6 +1438,8 @@ pub const Type = extern union {
14111438 .many_mut_pointer,
14121439 .c_const_pointer,
14131440 .c_mut_pointer,
1441 .const_slice,
1442 .mut_slice,
14141443 .single_const_pointer_to_comptime_int,
14151444 .const_slice_u8,
14161445 .int_unsigned,
......@@ -1476,6 +1505,8 @@ pub const Type = extern union {
14761505 .many_mut_pointer,
14771506 .c_const_pointer,
14781507 .c_mut_pointer,
1508 .const_slice,
1509 .mut_slice,
14791510 .single_const_pointer_to_comptime_int,
14801511 .const_slice_u8,
14811512 .int_unsigned,
......@@ -1531,6 +1562,8 @@ pub const Type = extern union {
15311562 .many_mut_pointer,
15321563 .c_const_pointer,
15331564 .c_mut_pointer,
1565 .const_slice,
1566 .mut_slice,
15341567 .single_const_pointer_to_comptime_int,
15351568 .const_slice_u8,
15361569 .int_unsigned,
......@@ -1597,6 +1630,8 @@ pub const Type = extern union {
15971630 .many_mut_pointer,
15981631 .c_const_pointer,
15991632 .c_mut_pointer,
1633 .const_slice,
1634 .mut_slice,
16001635 .single_const_pointer_to_comptime_int,
16011636 .const_slice_u8,
16021637 .int_signed,
......@@ -1663,6 +1698,8 @@ pub const Type = extern union {
16631698 .many_mut_pointer,
16641699 .c_const_pointer,
16651700 .c_mut_pointer,
1701 .const_slice,
1702 .mut_slice,
16661703 .single_const_pointer_to_comptime_int,
16671704 .const_slice_u8,
16681705 .optional,
......@@ -1727,6 +1764,8 @@ pub const Type = extern union {
17271764 .many_mut_pointer,
17281765 .c_const_pointer,
17291766 .c_mut_pointer,
1767 .const_slice,
1768 .mut_slice,
17301769 .single_const_pointer_to_comptime_int,
17311770 .const_slice_u8,
17321771 .int_unsigned,
......@@ -1820,6 +1859,8 @@ pub const Type = extern union {
18201859 .many_mut_pointer,
18211860 .c_const_pointer,
18221861 .c_mut_pointer,
1862 .const_slice,
1863 .mut_slice,
18231864 .single_const_pointer_to_comptime_int,
18241865 .const_slice_u8,
18251866 .u8,
......@@ -1889,6 +1930,8 @@ pub const Type = extern union {
18891930 .many_mut_pointer,
18901931 .c_const_pointer,
18911932 .c_mut_pointer,
1933 .const_slice,
1934 .mut_slice,
18921935 .single_const_pointer_to_comptime_int,
18931936 .const_slice_u8,
18941937 .u8,
......@@ -1957,6 +2000,8 @@ pub const Type = extern union {
19572000 .many_mut_pointer,
19582001 .c_const_pointer,
19592002 .c_mut_pointer,
2003 .const_slice,
2004 .mut_slice,
19602005 .single_const_pointer_to_comptime_int,
19612006 .const_slice_u8,
19622007 .u8,
......@@ -2025,6 +2070,8 @@ pub const Type = extern union {
20252070 .many_mut_pointer,
20262071 .c_const_pointer,
20272072 .c_mut_pointer,
2073 .const_slice,
2074 .mut_slice,
20282075 .single_const_pointer_to_comptime_int,
20292076 .const_slice_u8,
20302077 .u8,
......@@ -2090,6 +2137,8 @@ pub const Type = extern union {
20902137 .many_mut_pointer,
20912138 .c_const_pointer,
20922139 .c_mut_pointer,
2140 .const_slice,
2141 .mut_slice,
20932142 .single_const_pointer_to_comptime_int,
20942143 .const_slice_u8,
20952144 .u8,
......@@ -2155,6 +2204,8 @@ pub const Type = extern union {
21552204 .many_mut_pointer,
21562205 .c_const_pointer,
21572206 .c_mut_pointer,
2207 .const_slice,
2208 .mut_slice,
21582209 .single_const_pointer_to_comptime_int,
21592210 .const_slice_u8,
21602211 .u8,
......@@ -2240,6 +2291,8 @@ pub const Type = extern union {
22402291 .many_mut_pointer,
22412292 .c_const_pointer,
22422293 .c_mut_pointer,
2294 .const_slice,
2295 .mut_slice,
22432296 .single_const_pointer_to_comptime_int,
22442297 .const_slice_u8,
22452298 .optional,
......@@ -2290,6 +2343,8 @@ pub const Type = extern union {
22902343 .array_sentinel,
22912344 .array_u8_sentinel_0,
22922345 .const_slice_u8,
2346 .const_slice,
2347 .mut_slice,
22932348 .c_void,
22942349 .optional,
22952350 .optional_single_mut_pointer,
......@@ -2392,6 +2447,8 @@ pub const Type = extern union {
23922447 .single_mut_pointer,
23932448 .many_const_pointer,
23942449 .many_mut_pointer,
2450 .const_slice,
2451 .mut_slice,
23952452 .optional,
23962453 .optional_single_mut_pointer,
23972454 .optional_single_const_pointer,
......@@ -2467,6 +2524,8 @@ pub const Type = extern union {
24672524 many_mut_pointer,
24682525 c_const_pointer,
24692526 c_mut_pointer,
2527 const_slice,
2528 mut_slice,
24702529 int_signed,
24712530 int_unsigned,
24722531 function,
src-self-hosted/zir.zig+14-6
......@@ -194,18 +194,22 @@ pub const Inst = struct {
194194 shl,
195195 /// Integer shift-right. Arithmetic or logical depending on the signedness of the integer type.
196196 shr,
197 /// Create a const pointer type based on the element type. `*const T`
197 /// Create a const pointer type with element type T. `*const T`
198198 single_const_ptr_type,
199 /// Create a mutable pointer type based on the element type. `*T`
199 /// Create a mutable pointer type with element type T. `*T`
200200 single_mut_ptr_type,
201 /// Create a const pointer type based on the element type. `[*]const T`
201 /// Create a const pointer type with element type T. `[*]const T`
202202 many_const_ptr_type,
203 /// Create a mutable pointer type based on the element type. `[*]T`
203 /// Create a mutable pointer type with element type T. `[*]T`
204204 many_mut_ptr_type,
205 /// Create a const pointer type based on the element type. `[*c]const T`
205 /// Create a const pointer type with element type T. `[*c]const T`
206206 c_const_ptr_type,
207 /// Create a mutable pointer type based on the element type. `[*c]T`
207 /// Create a mutable pointer type with element type T. `[*c]T`
208208 c_mut_ptr_type,
209 /// Create a mutable slice type with element type T. `[]T`
210 mut_slice_type,
211 /// Create a const slice type with element type T. `[]T`
212 const_slice_type,
209213 /// Create a pointer type with attributes
210214 ptr_type,
211215 /// Write a value to a pointer. For loading, see `deref`.
......@@ -274,6 +278,8 @@ pub const Inst = struct {
274278 .many_mut_ptr_type,
275279 .c_const_ptr_type,
276280 .c_mut_ptr_type,
281 .mut_slice_type,
282 .const_slice_type,
277283 .optional_type,
278284 .unwrap_optional_safe,
279285 .unwrap_optional_unsafe,
......@@ -416,6 +422,8 @@ pub const Inst = struct {
416422 .many_mut_ptr_type,
417423 .c_const_ptr_type,
418424 .c_mut_ptr_type,
425 .mut_slice_type,
426 .const_slice_type,
419427 .store,
420428 .str,
421429 .sub,
src-self-hosted/zir_sema.zig+2
......@@ -57,6 +57,8 @@ pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!
5757 .many_mut_ptr_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.many_mut_ptr_type).?, true, .Many),
5858 .c_const_ptr_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.c_const_ptr_type).?, false, .C),
5959 .c_mut_ptr_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.c_mut_ptr_type).?, true, .C),
60 .const_slice_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.const_slice_type).?, false, .Slice),
61 .mut_slice_type => return analyzeInstSimplePtrType(mod, scope, old_inst.castTag(.mut_slice_type).?, true, .Slice),
6062 .ptr_type => return analyzeInstPtrType(mod, scope, old_inst.castTag(.ptr_type).?),
6163 .store => return analyzeInstStore(mod, scope, old_inst.castTag(.store).?),
6264 .str => return analyzeInstStr(mod, scope, old_inst.castTag(.str).?),