authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-23 04:45:35-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-23 04:45:35-05:00
log7597735badd1f6aa6750f354a7e9c85fec705c55
tree69e5e1b3795afcf065b0a40203ba4f678a4532d7
parent6b623b5ea2a811b54a2391f17081a8981fa733a5
signaturelock-open Commit is signed but in an unrecognized format.

update the stage1 implementation to the new proposal

See #3731

21 files changed, 614 insertions(+), 443 deletions(-)

lib/std/builtin.zig+12-2
......@@ -144,7 +144,12 @@ pub const TypeInfo = union(enum) {
144144 alignment: comptime_int,
145145 child: type,
146146 is_allowzero: bool,
147 is_null_terminated: bool,
147 /// The type of the sentinel is the element type of the pointer, which is
148 /// the value of the `child` field in this struct. However there is no way
149 /// to refer to that type here, so this is a pointer to an opaque value.
150 /// It will be known at compile-time to be the correct type. Dereferencing
151 /// this pointer will work at compile-time.
152 sentinel: ?*const c_void,
148153
149154 /// This data structure is used by the Zig language code generation and
150155 /// therefore must be kept in sync with the compiler implementation.
......@@ -161,7 +166,12 @@ pub const TypeInfo = union(enum) {
161166 pub const Array = struct {
162167 len: comptime_int,
163168 child: type,
164 is_null_terminated: bool,
169 /// The type of the sentinel is the element type of the array, which is
170 /// the value of the `child` field in this struct. However there is no way
171 /// to refer to that type here, so this is a pointer to an opaque value.
172 /// It will be known at compile-time to be the correct type. Dereferencing
173 /// this pointer will work at compile-time.
174 sentinel: ?*const c_void,
165175 };
166176
167177 /// This data structure is used by the Zig language code generation and
lib/std/c.zig+1-1
......@@ -63,7 +63,7 @@ pub extern "c" fn fclose(stream: *FILE) c_int;
6363pub extern "c" fn fwrite(ptr: [*]const u8, size_of_type: usize, item_count: usize, stream: *FILE) usize;
6464pub extern "c" fn fread(ptr: [*]u8, size_of_type: usize, item_count: usize, stream: *FILE) usize;
6565
66pub extern "c" fn printf(format: [*]null const u8, ...) c_int;
66pub extern "c" fn printf(format: [*:0]const u8, ...) c_int;
6767pub extern "c" fn abort() noreturn;
6868pub extern "c" fn exit(code: c_int) noreturn;
6969pub extern "c" fn isatty(fd: fd_t) c_int;
lib/std/mem.zig+1-1
......@@ -1409,7 +1409,7 @@ fn BytesAsValueReturnType(comptime T: type, comptime B: type) type {
14091409 const size = @as(usize, @sizeOf(T));
14101410
14111411 if (comptime !trait.is(builtin.TypeId.Pointer)(B) or
1412 (meta.Child(B) != [size]u8 and meta.Child(B) != [size]null u8))
1412 (meta.Child(B) != [size]u8 and meta.Child(B) != [size:0]u8))
14131413 {
14141414 @compileError("expected *[N]u8 " ++ ", passed " ++ @typeName(B));
14151415 }
lib/std/zig/parser_test.zig+3-3
......@@ -1552,7 +1552,7 @@ test "zig fmt: pointer attributes" {
15521552 \\extern fn f2(s: **align(1) *const *volatile u8) c_int;
15531553 \\extern fn f3(s: *align(1) const *align(1) volatile *const volatile u8) c_int;
15541554 \\extern fn f4(s: *align(1) const volatile u8) c_int;
1555 \\extern fn f5(s: [*]null align(1) const volatile u8) c_int;
1555 \\extern fn f5(s: [*:0]align(1) const volatile u8) c_int;
15561556 \\
15571557 );
15581558}
......@@ -1563,7 +1563,7 @@ test "zig fmt: slice attributes" {
15631563 \\extern fn f2(s: **align(1) *const *volatile u8) c_int;
15641564 \\extern fn f3(s: *align(1) const *align(1) volatile *const volatile u8) c_int;
15651565 \\extern fn f4(s: *align(1) const volatile u8) c_int;
1566 \\extern fn f5(s: [*]null align(1) const volatile u8) c_int;
1566 \\extern fn f5(s: [*:0]align(1) const volatile u8) c_int;
15671567 \\
15681568 );
15691569}
......@@ -1885,7 +1885,7 @@ test "zig fmt: arrays" {
18851885 \\ 2,
18861886 \\ };
18871887 \\ const a: [0]u8 = []u8{};
1888 \\ const x: [4]null u8 = undefined;
1888 \\ const x: [4:0]u8 = undefined;
18891889 \\}
18901890 \\
18911891 );
src/all_types.hpp+19-8
......@@ -55,7 +55,6 @@ enum PtrLen {
5555 PtrLenUnknown,
5656 PtrLenSingle,
5757 PtrLenC,
58 PtrLenNull,
5958};
6059
6160// This one corresponds to the builtin.zig enum.
......@@ -353,19 +352,20 @@ struct LazyValueSliceType {
353352 LazyValue base;
354353
355354 IrAnalyze *ira;
355 IrInstruction *sentinel; // can be null
356356 IrInstruction *elem_type;
357357 IrInstruction *align_inst; // can be null
358358
359359 bool is_const;
360360 bool is_volatile;
361361 bool is_allowzero;
362 bool is_null_terminated;
363362};
364363
365364struct LazyValuePtrType {
366365 LazyValue base;
367366
368367 IrAnalyze *ira;
368 IrInstruction *sentinel; // can be null
369369 IrInstruction *elem_type;
370370 IrInstruction *align_inst; // can be null
371371
......@@ -821,6 +821,7 @@ struct AstNodePrefixOpExpr {
821821
822822struct AstNodePointerType {
823823 Token *star_token;
824 AstNode *sentinel;
824825 AstNode *align_expr;
825826 BigInt *bit_offset_start;
826827 BigInt *host_int_bytes;
......@@ -828,21 +829,21 @@ struct AstNodePointerType {
828829 Token *allow_zero_token;
829830 bool is_const;
830831 bool is_volatile;
831 bool is_null_terminated;
832832};
833833
834834struct AstNodeInferredArrayType {
835 AstNode *sentinel; // can be null
835836 AstNode *child_type;
836837};
837838
838839struct AstNodeArrayType {
839840 AstNode *size;
841 AstNode *sentinel;
840842 AstNode *child_type;
841843 AstNode *align_expr;
842844 Token *allow_zero_token;
843845 bool is_const;
844846 bool is_volatile;
845 bool is_null_terminated;
846847};
847848
848849struct AstNodeUsingNamespace {
......@@ -1208,6 +1209,11 @@ struct ZigTypePointer {
12081209 // struct.
12091210 InferredStructField *inferred_struct_field;
12101211
1212 // This can be null. If it is non-null, it means the pointer is terminated by this
1213 // sentinel value. This is most commonly used for C-style strings, with a 0 byte
1214 // to specify the length of the memory pointed to.
1215 ConstExprValue *sentinel;
1216
12111217 PtrLen ptr_len;
12121218 uint32_t explicit_alignment; // 0 means use ABI alignment
12131219
......@@ -1235,7 +1241,7 @@ struct ZigTypeFloat {
12351241struct ZigTypeArray {
12361242 ZigType *child_type;
12371243 uint64_t len;
1238 bool is_null_terminated;
1244 ConstExprValue *sentinel;
12391245};
12401246
12411247struct TypeStructField {
......@@ -1761,8 +1767,10 @@ struct TypeId {
17611767
17621768 union {
17631769 struct {
1770 CodeGen *codegen;
17641771 ZigType *child_type;
17651772 InferredStructField *inferred_struct_field;
1773 ConstExprValue *sentinel;
17661774 PtrLen ptr_len;
17671775 uint32_t alignment;
17681776
......@@ -1775,9 +1783,10 @@ struct TypeId {
17751783 bool allow_zero;
17761784 } pointer;
17771785 struct {
1786 CodeGen *codegen;
17781787 ZigType *child_type;
17791788 uint64_t size;
1780 bool is_null_terminated;
1789 ConstExprValue *sentinel;
17811790 } array;
17821791 struct {
17831792 bool is_signed;
......@@ -2033,6 +2042,7 @@ struct CodeGen {
20332042 IrInstruction *invalid_instruction;
20342043 IrInstruction *unreach_instruction;
20352044
2045 ConstExprValue const_zero_byte;
20362046 ConstExprValue const_void_val;
20372047 ConstExprValue panic_msg_vals[PanicMsgIdCount];
20382048
......@@ -2989,13 +2999,14 @@ struct IrInstructionArrayType {
29892999 IrInstruction base;
29903000
29913001 IrInstruction *size;
3002 IrInstruction *sentinel;
29923003 IrInstruction *child_type;
2993 bool is_null_terminated;
29943004};
29953005
29963006struct IrInstructionPtrType {
29973007 IrInstruction base;
29983008
3009 IrInstruction *sentinel;
29993010 IrInstruction *align_value;
30003011 IrInstruction *child_type;
30013012 uint32_t bit_offset_start;
......@@ -3015,12 +3026,12 @@ struct IrInstructionAnyFrameType {
30153026struct IrInstructionSliceType {
30163027 IrInstruction base;
30173028
3029 IrInstruction *sentinel;
30183030 IrInstruction *align_value;
30193031 IrInstruction *child_type;
30203032 bool is_const;
30213033 bool is_volatile;
30223034 bool is_allow_zero;
3023 bool is_null_terminated;
30243035};
30253036
30263037struct IrInstructionGlobalAsm {
src/analyze.cpp+117-104
......@@ -452,20 +452,6 @@ ZigType *get_any_frame_type(CodeGen *g, ZigType *result_type) {
452452 return entry;
453453}
454454
455static const char *ptr_len_to_star_str(PtrLen ptr_len) {
456 switch (ptr_len) {
457 case PtrLenSingle:
458 return "*";
459 case PtrLenUnknown:
460 return "[*]";
461 case PtrLenC:
462 return "[*c]";
463 case PtrLenNull:
464 return "[*]null ";
465 }
466 zig_unreachable();
467}
468
469455ZigType *get_fn_frame_type(CodeGen *g, ZigFn *fn) {
470456 if (fn->frame_type != nullptr) {
471457 return fn->frame_type;
......@@ -485,10 +471,47 @@ ZigType *get_fn_frame_type(CodeGen *g, ZigFn *fn) {
485471 return entry;
486472}
487473
474static void append_ptr_type_attrs(Buf *type_name, ZigType *ptr_type) {
475 const char *const_str = ptr_type->data.pointer.is_const ? "const " : "";
476 const char *volatile_str = ptr_type->data.pointer.is_volatile ? "volatile " : "";
477 const char *allow_zero_str;
478 if (ptr_type->data.pointer.ptr_len == PtrLenC) {
479 assert(ptr_type->data.pointer.allow_zero);
480 allow_zero_str = "";
481 } else {
482 allow_zero_str = ptr_type->data.pointer.allow_zero ? "allowzero " : "";
483 }
484 if (ptr_type->data.pointer.explicit_alignment != 0 || ptr_type->data.pointer.host_int_bytes != 0 ||
485 ptr_type->data.pointer.vector_index != VECTOR_INDEX_NONE)
486 {
487 buf_appendf(type_name, "align(");
488 if (ptr_type->data.pointer.explicit_alignment != 0) {
489 buf_appendf(type_name, "%" PRIu32, ptr_type->data.pointer.explicit_alignment);
490 }
491 if (ptr_type->data.pointer.host_int_bytes != 0) {
492 buf_appendf(type_name, ":%" PRIu32 ":%" PRIu32, ptr_type->data.pointer.bit_offset_in_host, ptr_type->data.pointer.host_int_bytes);
493 }
494 if (ptr_type->data.pointer.vector_index == VECTOR_INDEX_RUNTIME) {
495 buf_appendf(type_name, ":?");
496 } else if (ptr_type->data.pointer.vector_index != VECTOR_INDEX_NONE) {
497 buf_appendf(type_name, ":%" PRIu32, ptr_type->data.pointer.vector_index);
498 }
499 buf_appendf(type_name, ")");
500 }
501 buf_appendf(type_name, "%s%s%s", const_str, volatile_str, allow_zero_str);
502 if (ptr_type->data.pointer.inferred_struct_field != nullptr) {
503 buf_appendf(type_name, " field '%s' of %s)",
504 buf_ptr(ptr_type->data.pointer.inferred_struct_field->field_name),
505 buf_ptr(&ptr_type->data.pointer.inferred_struct_field->inferred_struct_type->name));
506 } else {
507 buf_appendf(type_name, "%s", buf_ptr(&ptr_type->data.pointer.child_type->name));
508 }
509}
510
488511ZigType *get_pointer_to_type_extra2(CodeGen *g, ZigType *child_type, bool is_const,
489512 bool is_volatile, PtrLen ptr_len, uint32_t byte_alignment,
490513 uint32_t bit_offset_in_host, uint32_t host_int_bytes, bool allow_zero,
491 uint32_t vector_index, InferredStructField *inferred_struct_field)
514 uint32_t vector_index, InferredStructField *inferred_struct_field, ConstExprValue *sentinel)
492515{
493516 assert(ptr_len != PtrLenC || allow_zero);
494517 assert(!type_is_invalid(child_type));
......@@ -511,9 +534,11 @@ ZigType *get_pointer_to_type_extra2(CodeGen *g, ZigType *child_type, bool is_con
511534 TypeId type_id = {};
512535 ZigType **parent_pointer = nullptr;
513536 if (host_int_bytes != 0 || is_volatile || byte_alignment != 0 || ptr_len != PtrLenSingle ||
514 allow_zero || vector_index != VECTOR_INDEX_NONE || inferred_struct_field != nullptr)
537 allow_zero || vector_index != VECTOR_INDEX_NONE || inferred_struct_field != nullptr ||
538 sentinel != nullptr)
515539 {
516540 type_id.id = ZigTypeIdPointer;
541 type_id.data.pointer.codegen = g;
517542 type_id.data.pointer.child_type = child_type;
518543 type_id.data.pointer.is_const = is_const;
519544 type_id.data.pointer.is_volatile = is_volatile;
......@@ -524,6 +549,7 @@ ZigType *get_pointer_to_type_extra2(CodeGen *g, ZigType *child_type, bool is_con
524549 type_id.data.pointer.allow_zero = allow_zero;
525550 type_id.data.pointer.vector_index = vector_index;
526551 type_id.data.pointer.inferred_struct_field = inferred_struct_field;
552 type_id.data.pointer.sentinel = sentinel;
527553
528554 auto existing_entry = g->type_table.maybe_get(type_id);
529555 if (existing_entry)
......@@ -539,57 +565,36 @@ ZigType *get_pointer_to_type_extra2(CodeGen *g, ZigType *child_type, bool is_con
539565
540566 ZigType *entry = new_type_table_entry(ZigTypeIdPointer);
541567
542 const char *star_str = ptr_len_to_star_str(ptr_len);
543 const char *const_str = is_const ? "const " : "";
544 const char *volatile_str = is_volatile ? "volatile " : "";
545 const char *allow_zero_str;
546 if (ptr_len == PtrLenC) {
547 assert(allow_zero);
548 allow_zero_str = "";
549 } else {
550 allow_zero_str = allow_zero ? "allowzero " : "";
551 }
552568 buf_resize(&entry->name, 0);
553 if (host_int_bytes == 0 && byte_alignment == 0 && vector_index == VECTOR_INDEX_NONE) {
554 if (inferred_struct_field == nullptr) {
555 buf_appendf(&entry->name, "%s%s%s%s%s",
556 star_str, const_str, volatile_str, allow_zero_str, buf_ptr(&child_type->name));
557 } else {
558 buf_appendf(&entry->name, "(%s%s%s%s field '%s' of %s)",
559 star_str, const_str, volatile_str, allow_zero_str,
560 buf_ptr(inferred_struct_field->field_name),
561 buf_ptr(&inferred_struct_field->inferred_struct_type->name));
562 }
563 } else if (host_int_bytes == 0 && vector_index == VECTOR_INDEX_NONE) {
564 buf_appendf(&entry->name, "%salign(%" PRIu32 ") %s%s%s%s", star_str, byte_alignment,
565 const_str, volatile_str, allow_zero_str, buf_ptr(&child_type->name));
566 } else if (byte_alignment == 0) {
567 assert(vector_index == VECTOR_INDEX_NONE);
568 buf_appendf(&entry->name, "%salign(:%" PRIu32 ":%" PRIu32 ") %s%s%s%s",
569 star_str,
570 bit_offset_in_host, host_int_bytes,
571 const_str, volatile_str, allow_zero_str,
572 buf_ptr(&child_type->name));
573 } else if (vector_index == VECTOR_INDEX_NONE) {
574 buf_appendf(&entry->name, "%salign(%" PRIu32 ":%" PRIu32 ":%" PRIu32 ") %s%s%s%s",
575 star_str, byte_alignment,
576 bit_offset_in_host, host_int_bytes,
577 const_str, volatile_str, allow_zero_str,
578 buf_ptr(&child_type->name));
579 } else if (vector_index == VECTOR_INDEX_RUNTIME) {
580 buf_appendf(&entry->name, "%salign(%" PRIu32 ":%" PRIu32 ":%" PRIu32 ":?) %s%s%s%s",
581 star_str, byte_alignment,
582 bit_offset_in_host, host_int_bytes,
583 const_str, volatile_str, allow_zero_str,
584 buf_ptr(&child_type->name));
585 } else {
586 buf_appendf(&entry->name, "%salign(%" PRIu32 ":%" PRIu32 ":%" PRIu32 ":%" PRIu32 ") %s%s%s%s",
587 star_str, byte_alignment,
588 bit_offset_in_host, host_int_bytes, vector_index,
589 const_str, volatile_str, allow_zero_str,
590 buf_ptr(&child_type->name));
569 if (inferred_struct_field != nullptr) {
570 buf_appendf(&entry->name, "(");
571 }
572 switch (ptr_len) {
573 case PtrLenSingle:
574 buf_appendf(&entry->name, "*");
575 break;
576 case PtrLenUnknown:
577 buf_appendf(&entry->name, "[*");
578 break;
579 case PtrLenC:
580 assert(sentinel == nullptr);
581 buf_appendf(&entry->name, "[*c]");
582 break;
583 }
584 if (sentinel != nullptr) {
585 buf_appendf(&entry->name, ":");
586 render_const_value(g, &entry->name, sentinel);
587 }
588 switch (ptr_len) {
589 case PtrLenSingle:
590 case PtrLenC:
591 break;
592 case PtrLenUnknown:
593 buf_appendf(&entry->name, "]");
594 break;
591595 }
592596
597
593598 if (type_is_resolved(child_type, ResolveStatusZeroBitsKnown)) {
594599 if (type_has_bits(child_type)) {
595600 entry->abi_size = g->builtin_types.entry_usize->abi_size;
......@@ -617,6 +622,9 @@ ZigType *get_pointer_to_type_extra2(CodeGen *g, ZigType *child_type, bool is_con
617622 entry->data.pointer.allow_zero = allow_zero;
618623 entry->data.pointer.vector_index = vector_index;
619624 entry->data.pointer.inferred_struct_field = inferred_struct_field;
625 entry->data.pointer.sentinel = sentinel;
626
627 append_ptr_type_attrs(&entry->name, entry);
620628
621629 if (parent_pointer) {
622630 *parent_pointer = entry;
......@@ -631,12 +639,12 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type, bool is_cons
631639 uint32_t bit_offset_in_host, uint32_t host_int_bytes, bool allow_zero)
632640{
633641 return get_pointer_to_type_extra2(g, child_type, is_const, is_volatile, ptr_len,
634 byte_alignment, bit_offset_in_host, host_int_bytes, allow_zero, VECTOR_INDEX_NONE, nullptr);
642 byte_alignment, bit_offset_in_host, host_int_bytes, allow_zero, VECTOR_INDEX_NONE, nullptr, nullptr);
635643}
636644
637645ZigType *get_pointer_to_type(CodeGen *g, ZigType *child_type, bool is_const) {
638646 return get_pointer_to_type_extra2(g, child_type, is_const, false, PtrLenSingle, 0, 0, 0, false,
639 VECTOR_INDEX_NONE, nullptr);
647 VECTOR_INDEX_NONE, nullptr, nullptr);
640648}
641649
642650ZigType *get_optional_type(CodeGen *g, ZigType *child_type) {
......@@ -752,12 +760,13 @@ ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payloa
752760 return entry;
753761}
754762
755ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size, bool is_null_terminated) {
763ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size, ConstExprValue *sentinel) {
756764 TypeId type_id = {};
757765 type_id.id = ZigTypeIdArray;
766 type_id.data.array.codegen = g;
758767 type_id.data.array.child_type = child_type;
759768 type_id.data.array.size = array_size;
760 type_id.data.array.is_null_terminated = is_null_terminated;
769 type_id.data.array.sentinel = sentinel;
761770 auto existing_entry = g->type_table.maybe_get(type_id);
762771 if (existing_entry) {
763772 return existing_entry->value;
......@@ -767,16 +776,19 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size, bo
767776
768777 ZigType *entry = new_type_table_entry(ZigTypeIdArray);
769778
770 const char *null_str = is_null_terminated ? "null " : "";
771
772779 buf_resize(&entry->name, 0);
773 buf_appendf(&entry->name, "[%" ZIG_PRI_u64 "]%s%s", array_size, null_str, buf_ptr(&child_type->name));
780 buf_appendf(&entry->name, "[%" ZIG_PRI_u64, array_size);
781 if (sentinel != nullptr) {
782 buf_appendf(&entry->name, ":");
783 render_const_value(g, &entry->name, sentinel);
784 }
785 buf_appendf(&entry->name, "]%s", buf_ptr(&child_type->name));
774786
775787 size_t full_array_size;
776788 if (array_size == 0) {
777789 full_array_size = 0;
778790 } else {
779 full_array_size = array_size + (is_null_terminated ? 1 : 0);
791 full_array_size = array_size + ((sentinel != nullptr) ? 1 : 0);
780792 }
781793
782794 entry->size_in_bits = child_type->size_in_bits * full_array_size;
......@@ -785,7 +797,7 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size, bo
785797
786798 entry->data.array.child_type = child_type;
787799 entry->data.array.len = array_size;
788 entry->data.array.is_null_terminated = is_null_terminated;
800 entry->data.array.sentinel = sentinel;
789801
790802 g->type_table.put(type_id, entry);
791803 return entry;
......@@ -793,7 +805,7 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size, bo
793805
794806ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
795807 assert(ptr_type->id == ZigTypeIdPointer);
796 assert(ptr_type->data.pointer.ptr_len == PtrLenUnknown || ptr_type->data.pointer.ptr_len == PtrLenNull);
808 assert(ptr_type->data.pointer.ptr_len == PtrLenUnknown);
797809
798810 ZigType **parent_pointer = &ptr_type->data.pointer.slice_parent;
799811 if (*parent_pointer) {
......@@ -802,10 +814,14 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
802814
803815 ZigType *entry = new_type_table_entry(ZigTypeIdStruct);
804816
805 // replace the & with [] to go from a ptr type name to a slice type name
806817 buf_resize(&entry->name, 0);
807 size_t name_offset = (ptr_type->data.pointer.ptr_len == PtrLenSingle) ? 1 : 3;
808 buf_appendf(&entry->name, "[]%s", buf_ptr(&ptr_type->name) + name_offset);
818 buf_appendf(&entry->name, "[");
819 if (ptr_type->data.pointer.sentinel != nullptr) {
820 buf_appendf(&entry->name, ":");
821 render_const_value(g, &entry->name, ptr_type->data.pointer.sentinel);
822 }
823 buf_appendf(&entry->name, "]");
824 append_ptr_type_attrs(&entry->name, ptr_type);
809825
810826 unsigned element_count = 2;
811827 Buf *ptr_field_name = buf_create_from_str("ptr");
......@@ -5639,14 +5655,14 @@ void init_const_str_lit(CodeGen *g, ConstExprValue *const_val, Buf *str) {
56395655 // first we build the underlying array
56405656 ConstExprValue *array_val = create_const_vals(1);
56415657 array_val->special = ConstValSpecialStatic;
5642 array_val->type = get_array_type(g, g->builtin_types.entry_u8, buf_len(str), true);
5658 array_val->type = get_array_type(g, g->builtin_types.entry_u8, buf_len(str), &g->const_zero_byte);
56435659 array_val->data.x_array.special = ConstArraySpecialBuf;
56445660 array_val->data.x_array.data.s_buf = str;
56455661
56465662 // then make the pointer point to it
56475663 const_val->special = ConstValSpecialStatic;
56485664 const_val->type = get_pointer_to_type_extra2(g, array_val->type, true, false,
5649 PtrLenSingle, 0, 0, 0, false, VECTOR_INDEX_NONE, nullptr);
5665 PtrLenSingle, 0, 0, 0, false, VECTOR_INDEX_NONE, nullptr, nullptr);
56505666 const_val->data.x_ptr.special = ConstPtrSpecialRef;
56515667 const_val->data.x_ptr.data.ref.pointee = array_val;
56525668
......@@ -6079,7 +6095,7 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {
60796095
60806096 fields.append({"@stack_trace", get_stack_trace_type(g), 0});
60816097 fields.append({"@instruction_addresses",
6082 get_array_type(g, g->builtin_types.entry_usize, stack_trace_ptr_count, false), 0});
6098 get_array_type(g, g->builtin_types.entry_usize, stack_trace_ptr_count, nullptr), 0});
60836099 }
60846100
60856101 frame_type->data.frame.locals_struct = get_struct_type(g, buf_ptr(&frame_type->name),
......@@ -6287,7 +6303,7 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {
62876303 if (codegen_fn_has_err_ret_tracing_stack(g, fn, true)) {
62886304 fields.append({"@stack_trace", get_stack_trace_type(g), 0});
62896305 fields.append({"@instruction_addresses",
6290 get_array_type(g, g->builtin_types.entry_usize, stack_trace_ptr_count, false), 0});
6306 get_array_type(g, g->builtin_types.entry_usize, stack_trace_ptr_count, nullptr), 0});
62916307 }
62926308
62936309 for (size_t alloca_i = 0; alloca_i < fn->alloca_gen_list.length; alloca_i += 1) {
......@@ -7050,11 +7066,12 @@ uint32_t type_id_hash(TypeId x) {
70507066 (((uint32_t)x.data.pointer.alignment) ^ (uint32_t)0x777fbe0e) +
70517067 (((uint32_t)x.data.pointer.bit_offset_in_host) ^ (uint32_t)2639019452) +
70527068 (((uint32_t)x.data.pointer.vector_index) ^ (uint32_t)0x19199716) +
7053 (((uint32_t)x.data.pointer.host_int_bytes) ^ (uint32_t)529908881);
7069 (((uint32_t)x.data.pointer.host_int_bytes) ^ (uint32_t)529908881) *
7070 (x.data.pointer.sentinel ? hash_const_val(x.data.pointer.sentinel) : (uint32_t)2955491856);
70547071 case ZigTypeIdArray:
70557072 return hash_ptr(x.data.array.child_type) *
70567073 ((uint32_t)x.data.array.size ^ (uint32_t)2122979968) *
7057 ((uint32_t)x.data.array.is_null_terminated ^ (uint32_t)2048352596);
7074 (x.data.array.sentinel ? hash_const_val(x.data.array.sentinel) : (uint32_t)1927201585);
70587075 case ZigTypeIdInt:
70597076 return (x.data.integer.is_signed ? (uint32_t)2652528194 : (uint32_t)163929201) +
70607077 (((uint32_t)x.data.integer.bit_count) ^ (uint32_t)2998081557);
......@@ -7105,6 +7122,11 @@ bool type_id_eql(TypeId a, TypeId b) {
71057122 a.data.pointer.bit_offset_in_host == b.data.pointer.bit_offset_in_host &&
71067123 a.data.pointer.vector_index == b.data.pointer.vector_index &&
71077124 a.data.pointer.host_int_bytes == b.data.pointer.host_int_bytes &&
7125 (
7126 a.data.pointer.sentinel == b.data.pointer.sentinel ||
7127 (a.data.pointer.sentinel != nullptr && b.data.pointer.sentinel != nullptr &&
7128 const_values_equal(a.data.pointer.codegen, a.data.pointer.sentinel, b.data.pointer.sentinel))
7129 ) &&
71087130 (
71097131 a.data.pointer.inferred_struct_field == b.data.pointer.inferred_struct_field ||
71107132 (a.data.pointer.inferred_struct_field != nullptr &&
......@@ -7117,7 +7139,11 @@ bool type_id_eql(TypeId a, TypeId b) {
71177139 case ZigTypeIdArray:
71187140 return a.data.array.child_type == b.data.array.child_type &&
71197141 a.data.array.size == b.data.array.size &&
7120 a.data.array.is_null_terminated == b.data.array.is_null_terminated;
7142 (
7143 a.data.array.sentinel == b.data.array.sentinel ||
7144 (a.data.array.sentinel != nullptr && b.data.array.sentinel != nullptr &&
7145 const_values_equal(a.data.array.codegen, a.data.array.sentinel, b.data.array.sentinel))
7146 );
71217147 case ZigTypeIdInt:
71227148 return a.data.integer.is_signed == b.data.integer.is_signed &&
71237149 a.data.integer.bit_count == b.data.integer.bit_count;
......@@ -8303,7 +8329,7 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta
83038329 size_t padding_bytes = union_type->data.unionation.union_abi_size - most_aligned_union_member->type_entry->abi_size;
83048330 if (padding_bytes > 0) {
83058331 ZigType *u8_type = get_int_type(g, false, 8);
8306 ZigType *padding_array = get_array_type(g, u8_type, padding_bytes, false);
8332 ZigType *padding_array = get_array_type(g, u8_type, padding_bytes, nullptr);
83078333 LLVMTypeRef union_element_types[] = {
83088334 most_aligned_union_member->type_entry->llvm_type,
83098335 get_llvm_type(g, padding_array),
......@@ -8337,7 +8363,7 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta
83378363 union_type_ref = get_llvm_type(g, most_aligned_union_member->type_entry);
83388364 } else {
83398365 ZigType *u8_type = get_int_type(g, false, 8);
8340 ZigType *padding_array = get_array_type(g, u8_type, padding_bytes, false);
8366 ZigType *padding_array = get_array_type(g, u8_type, padding_bytes, nullptr);
83418367 LLVMTypeRef union_element_types[] = {
83428368 get_llvm_type(g, most_aligned_union_member->type_entry),
83438369 get_llvm_type(g, padding_array),
......@@ -8418,19 +8444,19 @@ static void resolve_llvm_types_pointer(CodeGen *g, ZigType *type, ResolveStatus
84188444 if (type->data.pointer.is_const || type->data.pointer.is_volatile ||
84198445 type->data.pointer.explicit_alignment != 0 || type->data.pointer.ptr_len != PtrLenSingle ||
84208446 type->data.pointer.bit_offset_in_host != 0 || type->data.pointer.allow_zero ||
8421 type->data.pointer.vector_index != VECTOR_INDEX_NONE)
8447 type->data.pointer.vector_index != VECTOR_INDEX_NONE || type->data.pointer.sentinel != nullptr)
84228448 {
84238449 assertNoError(type_resolve(g, elem_type, ResolveStatusLLVMFwdDecl));
84248450 ZigType *peer_type;
84258451 if (type->data.pointer.vector_index == VECTOR_INDEX_NONE) {
84268452 peer_type = get_pointer_to_type_extra2(g, elem_type, false, false,
84278453 PtrLenSingle, 0, 0, type->data.pointer.host_int_bytes, false,
8428 VECTOR_INDEX_NONE, nullptr);
8454 VECTOR_INDEX_NONE, nullptr, nullptr);
84298455 } else {
84308456 uint32_t host_vec_len = type->data.pointer.host_int_bytes;
84318457 ZigType *host_vec_type = get_vector_type(g, host_vec_len, elem_type);
84328458 peer_type = get_pointer_to_type_extra2(g, host_vec_type, false, false,
8433 PtrLenSingle, 0, 0, 0, false, VECTOR_INDEX_NONE, nullptr);
8459 PtrLenSingle, 0, 0, 0, false, VECTOR_INDEX_NONE, nullptr, nullptr);
84348460 }
84358461 type->llvm_type = get_llvm_type(g, peer_type);
84368462 type->llvm_di_type = get_llvm_di_type(g, peer_type);
......@@ -8659,8 +8685,8 @@ static void resolve_llvm_types_array(CodeGen *g, ZigType *type) {
86598685
86608686 ZigType *elem_type = type->data.array.child_type;
86618687
8662 uint64_t extra_len_from_null = type->data.array.is_null_terminated ? 1 : 0;
8663 uint64_t full_len = type->data.array.len + extra_len_from_null;
8688 uint64_t extra_len_from_sentinel = (type->data.array.sentinel != nullptr) ? 1 : 0;
8689 uint64_t full_len = type->data.array.len + extra_len_from_sentinel;
86648690 // TODO https://github.com/ziglang/zig/issues/1424
86658691 type->llvm_type = LLVMArrayType(get_llvm_type(g, elem_type), (unsigned)full_len);
86668692
......@@ -9166,16 +9192,3 @@ void IrExecutable::src() {
91669192 it->source_node->src();
91679193 }
91689194}
9169
9170ConstExprValue *get_null_value(ZigType *ty) {
9171 if (ty->id == ZigTypeIdInt || ty->id == ZigTypeIdComptimeInt) {
9172 return create_const_unsigned_negative(ty, 0, false);
9173 } else if (ty->id == ZigTypeIdFloat || ty->id == ZigTypeIdComptimeFloat) {
9174 return create_const_float(ty, NAN);
9175 } else if (ty->id == ZigTypeIdOptional) {
9176 return create_const_null(ty);
9177 } else {
9178 zig_unreachable();
9179 }
9180}
9181
src/analyze.hpp+3-3
......@@ -24,7 +24,8 @@ ZigType *get_pointer_to_type_extra(CodeGen *g, ZigType *child_type,
2424ZigType *get_pointer_to_type_extra2(CodeGen *g, ZigType *child_type,
2525 bool is_const, bool is_volatile, PtrLen ptr_len,
2626 uint32_t byte_alignment, uint32_t bit_offset, uint32_t unaligned_bit_count,
27 bool allow_zero, uint32_t vector_index, InferredStructField *inferred_struct_field);
27 bool allow_zero, uint32_t vector_index, InferredStructField *inferred_struct_field,
28 ConstExprValue *sentinel);
2829uint64_t type_size(CodeGen *g, ZigType *type_entry);
2930uint64_t type_size_bits(CodeGen *g, ZigType *type_entry);
3031ZigType *get_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits);
......@@ -33,7 +34,7 @@ ZigType **get_c_int_type_ptr(CodeGen *g, CIntType c_int_type);
3334ZigType *get_c_int_type(CodeGen *g, CIntType c_int_type);
3435ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id);
3536ZigType *get_optional_type(CodeGen *g, ZigType *child_type);
36ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size, bool is_null_terminated);
37ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size, ConstExprValue *sentinel);
3738ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type);
3839ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind,
3940 AstNode *decl_node, const char *full_name, Buf *bare_name, ContainerLayout layout);
......@@ -276,5 +277,4 @@ IrInstruction *ir_create_alloca(CodeGen *g, Scope *scope, AstNode *source_node,
276277Error analyze_import(CodeGen *codegen, ZigType *source_import, Buf *import_target_str,
277278 ZigType **out_import, Buf **out_import_target_path, Buf *out_full_path);
278279ConstExprValue *get_the_one_possible_value(CodeGen *g, ZigType *type_entry);
279ConstExprValue *get_null_value(ZigType *ty);
280280#endif
src/ast_render.cpp+2-2
......@@ -147,9 +147,9 @@ static const char *token_to_ptr_len_str(Token *tok) {
147147 case TokenIdStar:
148148 case TokenIdStarStar:
149149 return "*";
150 case TokenIdBracketStarBracket:
150 case TokenIdLBracket:
151151 return "[*]";
152 case TokenIdBracketStarCBracket:
152 case TokenIdSymbol:
153153 return "[*c]";
154154 default:
155155 zig_unreachable();
src/codegen.cpp+16-11
......@@ -3707,8 +3707,8 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI
37073707 array_type = array_type->data.pointer.child_type;
37083708 }
37093709 if (safety_check_on) {
3710 uint64_t extra_len_from_null = array_type->data.array.is_null_terminated ? 1 : 0;
3711 uint64_t full_len = array_type->data.array.len + extra_len_from_null;
3710 uint64_t extra_len_from_sentinel = (array_type->data.array.sentinel != nullptr) ? 1 : 0;
3711 uint64_t full_len = array_type->data.array.len + extra_len_from_sentinel;
37123712 LLVMValueRef end = LLVMConstInt(g->builtin_types.entry_usize->llvm_type, full_len, false);
37133713 add_bounds_check(g, subscript_value, LLVMIntEQ, nullptr, LLVMIntULT, end);
37143714 }
......@@ -6636,8 +6636,8 @@ static LLVMValueRef gen_const_val_ptr(CodeGen *g, ConstExprValue *const_val, con
66366636 ConstExprValue *array_const_val = const_val->data.x_ptr.data.base_array.array_val;
66376637 assert(array_const_val->type->id == ZigTypeIdArray);
66386638 if (!type_has_bits(array_const_val->type)) {
6639 if (array_const_val->type->data.array.is_null_terminated) {
6640 ConstExprValue *pointee = get_null_value(array_const_val->type->data.array.child_type);
6639 if (array_const_val->type->data.array.sentinel != nullptr) {
6640 ConstExprValue *pointee = array_const_val->type->data.array.sentinel;
66416641 render_const_val(g, pointee, "");
66426642 render_const_val_global(g, pointee, "");
66436643 const_val->global_refs->llvm_value = LLVMConstBitCast(pointee->global_refs->llvm_global,
......@@ -6963,8 +6963,8 @@ check: switch (const_val->special) {
69636963 case ConstArraySpecialUndef:
69646964 return LLVMGetUndef(get_llvm_type(g, type_entry));
69656965 case ConstArraySpecialNone: {
6966 uint64_t extra_len_from_null = type_entry->data.array.is_null_terminated ? 1 : 0;
6967 uint64_t full_len = len + extra_len_from_null;
6966 uint64_t extra_len_from_sentinel = (type_entry->data.array.sentinel != nullptr) ? 1 : 0;
6967 uint64_t full_len = len + extra_len_from_sentinel;
69686968 LLVMValueRef *values = allocate<LLVMValueRef>(full_len);
69696969 LLVMTypeRef element_type_ref = get_llvm_type(g, type_entry->data.array.child_type);
69706970 bool make_unnamed_struct = false;
......@@ -6974,8 +6974,8 @@ check: switch (const_val->special) {
69746974 values[i] = val;
69756975 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(g, elem_value->type, val);
69766976 }
6977 if (type_entry->data.array.is_null_terminated) {
6978 values[len] = LLVMConstNull(element_type_ref);
6977 if (type_entry->data.array.sentinel != nullptr) {
6978 values[len] = gen_const_val(g, type_entry->data.array.sentinel, "");
69796979 }
69806980 if (make_unnamed_struct) {
69816981 return LLVMConstStruct(values, full_len, true);
......@@ -6986,7 +6986,7 @@ check: switch (const_val->special) {
69866986 case ConstArraySpecialBuf: {
69876987 Buf *buf = const_val->data.x_array.data.s_buf;
69886988 return LLVMConstString(buf_ptr(buf), (unsigned)buf_len(buf),
6989 !type_entry->data.array.is_null_terminated);
6989 type_entry->data.array.sentinel == nullptr);
69906990 }
69916991 }
69926992 zig_unreachable();
......@@ -7479,7 +7479,7 @@ static void do_code_gen(CodeGen *g) {
74797479 !is_async && !have_err_ret_trace_arg;
74807480 LLVMValueRef err_ret_array_val = nullptr;
74817481 if (have_err_ret_trace_stack) {
7482 ZigType *array_type = get_array_type(g, g->builtin_types.entry_usize, stack_trace_ptr_count, false);
7482 ZigType *array_type = get_array_type(g, g->builtin_types.entry_usize, stack_trace_ptr_count, nullptr);
74837483 err_ret_array_val = build_alloca(g, array_type, "error_return_trace_addresses", get_abi_alignment(g, array_type));
74847484
74857485 (void)get_llvm_type(g, get_stack_trace_type(g));
......@@ -8642,6 +8642,11 @@ static void init(CodeGen *g) {
86428642 g->const_void_val.type = g->builtin_types.entry_void;
86438643 g->const_void_val.global_refs = allocate<ConstGlobalRefs>(1);
86448644
8645 g->const_zero_byte.special = ConstValSpecialStatic;
8646 g->const_zero_byte.type = g->builtin_types.entry_u8;
8647 g->const_zero_byte.global_refs = allocate<ConstGlobalRefs>(1);
8648 bigint_init_unsigned(&g->const_zero_byte.data.x_bigint, 0);
8649
86458650 {
86468651 ConstGlobalRefs *global_refs = allocate<ConstGlobalRefs>(PanicMsgIdCount);
86478652 for (size_t i = 0; i < PanicMsgIdCount; i += 1) {
......@@ -9081,7 +9086,7 @@ static void create_test_compile_var_and_add_test_runner(CodeGen *g) {
90819086 zig_unreachable();
90829087
90839088 ConstExprValue *test_fn_array = create_const_vals(1);
9084 test_fn_array->type = get_array_type(g, struct_type, g->test_fns.length, false);
9089 test_fn_array->type = get_array_type(g, struct_type, g->test_fns.length, nullptr);
90859090 test_fn_array->special = ConstValSpecialStatic;
90869091 test_fn_array->data.x_array.data.s_none.elements = create_const_vals(g->test_fns.length);
90879092
src/dump_analysis.cpp-4
......@@ -992,10 +992,6 @@ static void anal_dump_type(AnalDumpCtx *ctx, ZigType *ty) {
992992 jw_object_field(jw, "len");
993993 jw_int(jw, 3);
994994 break;
995 case PtrLenNull:
996 jw_object_field(jw, "len");
997 jw_int(jw, 4);
998 break;
999995 }
1000996 anal_dump_pointer_attrs(ctx, ty);
1001997 break;
src/ir.cpp+312-142
......@@ -67,9 +67,10 @@ enum ConstCastResultId {
6767 ConstCastResultIdAsyncAllocatorType,
6868 ConstCastResultIdBadAllowsZero,
6969 ConstCastResultIdArrayChild,
70 ConstCastResultIdBadNullTermArrays,
70 ConstCastResultIdSentinelArrays,
7171 ConstCastResultIdPtrLens,
7272 ConstCastResultIdCV,
73 ConstCastResultIdPtrSentinel,
7374};
7475
7576struct ConstCastOnly;
......@@ -94,8 +95,8 @@ struct ConstCastTypeMismatch;
9495struct ConstCastArrayMismatch;
9596struct ConstCastBadAllowsZero;
9697struct ConstCastBadNullTermArrays;
97struct ConstCastBadPtrLens;
9898struct ConstCastBadCV;
99struct ConstCastPtrSentinel;
99100
100101struct ConstCastOnly {
101102 ConstCastResultId id;
......@@ -113,9 +114,9 @@ struct ConstCastOnly {
113114 ConstCastArg fn_arg;
114115 ConstCastArgNoAlias arg_no_alias;
115116 ConstCastBadAllowsZero *bad_allows_zero;
116 ConstCastBadNullTermArrays *bad_null_term_arrays;
117 ConstCastBadPtrLens *bad_ptr_lens;
117 ConstCastBadNullTermArrays *sentinel_arrays;
118118 ConstCastBadCV *bad_cv;
119 ConstCastPtrSentinel *bad_ptr_sentinel;
119120 } data;
120121};
121122
......@@ -175,14 +176,13 @@ struct ConstCastBadNullTermArrays {
175176 ZigType *actual_type;
176177};
177178
178struct ConstCastBadPtrLens {
179struct ConstCastBadCV {
179180 ZigType *wanted_type;
180181 ZigType *actual_type;
181182};
182183
183struct ConstCastBadCV {
184struct ConstCastPtrSentinel {
184185 ZigType *wanted_type;
185 ZigType *actual_type;
186186};
187187
188188static IrInstruction *ir_gen_node(IrBuilder *irb, AstNode *node, Scope *scope);
......@@ -264,8 +264,7 @@ static ConstExprValue *const_ptr_pointee_unchecked(CodeGen *g, ConstExprValue *c
264264 case ConstPtrSpecialBaseArray: {
265265 ConstExprValue *array_val = const_val->data.x_ptr.data.base_array.array_val;
266266 if (const_val->data.x_ptr.data.base_array.elem_index == array_val->type->data.array.len) {
267 assert(array_val->type->data.array.is_null_terminated);
268 result = get_null_value(array_val->type->data.array.child_type);
267 result = array_val->type->data.array.sentinel;
269268 } else {
270269 expand_undef_array(g, array_val);
271270 result = &array_val->data.x_array.data.s_none.elements[const_val->data.x_ptr.data.base_array.elem_index];
......@@ -317,7 +316,7 @@ static bool slice_is_const(ZigType *type) {
317316
318317// This function returns true when you can change the type of a ConstExprValue and the
319318// value remains meaningful.
320static bool types_have_same_zig_comptime_repr(ZigType *expected, ZigType *actual) {
319static bool types_have_same_zig_comptime_repr(CodeGen *codegen, ZigType *expected, ZigType *actual) {
321320 if (expected == actual)
322321 return true;
323322
......@@ -366,7 +365,8 @@ static bool types_have_same_zig_comptime_repr(ZigType *expected, ZigType *actual
366365 case ZigTypeIdArray:
367366 return expected->data.array.len == actual->data.array.len &&
368367 expected->data.array.child_type == actual->data.array.child_type &&
369 (!expected->data.array.is_null_terminated || actual->data.array.is_null_terminated);
368 (expected->data.array.sentinel == nullptr || (actual->data.array.sentinel != nullptr &&
369 const_values_equal(codegen, expected->data.array.sentinel, actual->data.array.sentinel)));
370370 }
371371 zig_unreachable();
372372}
......@@ -1576,9 +1576,11 @@ static IrInstruction *ir_build_br(IrBuilder *irb, Scope *scope, AstNode *source_
15761576
15771577static IrInstruction *ir_build_ptr_type(IrBuilder *irb, Scope *scope, AstNode *source_node,
15781578 IrInstruction *child_type, bool is_const, bool is_volatile, PtrLen ptr_len,
1579 IrInstruction *align_value, uint32_t bit_offset_start, uint32_t host_int_bytes, bool is_allow_zero)
1579 IrInstruction *sentinel, IrInstruction *align_value,
1580 uint32_t bit_offset_start, uint32_t host_int_bytes, bool is_allow_zero)
15801581{
15811582 IrInstructionPtrType *ptr_type_of_instruction = ir_build_instruction<IrInstructionPtrType>(irb, scope, source_node);
1583 ptr_type_of_instruction->sentinel = sentinel;
15821584 ptr_type_of_instruction->align_value = align_value;
15831585 ptr_type_of_instruction->child_type = child_type;
15841586 ptr_type_of_instruction->is_const = is_const;
......@@ -1588,6 +1590,7 @@ static IrInstruction *ir_build_ptr_type(IrBuilder *irb, Scope *scope, AstNode *s
15881590 ptr_type_of_instruction->host_int_bytes = host_int_bytes;
15891591 ptr_type_of_instruction->is_allow_zero = is_allow_zero;
15901592
1593 if (sentinel) ir_ref_instruction(sentinel, irb->current_basic_block);
15911594 if (align_value) ir_ref_instruction(align_value, irb->current_basic_block);
15921595 ir_ref_instruction(child_type, irb->current_basic_block);
15931596
......@@ -1804,14 +1807,15 @@ static IrInstruction *ir_build_set_float_mode(IrBuilder *irb, Scope *scope, AstN
18041807}
18051808
18061809static IrInstruction *ir_build_array_type(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *size,
1807 IrInstruction *child_type, bool is_null_terminated)
1810 IrInstruction *sentinel, IrInstruction *child_type)
18081811{
18091812 IrInstructionArrayType *instruction = ir_build_instruction<IrInstructionArrayType>(irb, scope, source_node);
18101813 instruction->size = size;
1814 instruction->sentinel = sentinel;
18111815 instruction->child_type = child_type;
1812 instruction->is_null_terminated = is_null_terminated;
18131816
18141817 ir_ref_instruction(size, irb->current_basic_block);
1818 if (sentinel != nullptr) ir_ref_instruction(sentinel, irb->current_basic_block);
18151819 ir_ref_instruction(child_type, irb->current_basic_block);
18161820
18171821 return &instruction->base;
......@@ -1827,20 +1831,22 @@ static IrInstruction *ir_build_anyframe_type(IrBuilder *irb, Scope *scope, AstNo
18271831
18281832 return &instruction->base;
18291833}
1834
18301835static IrInstruction *ir_build_slice_type(IrBuilder *irb, Scope *scope, AstNode *source_node,
1831 IrInstruction *child_type, bool is_const, bool is_volatile, IrInstruction *align_value, bool is_allow_zero,
1832 bool is_null_terminated)
1836 IrInstruction *child_type, bool is_const, bool is_volatile,
1837 IrInstruction *sentinel, IrInstruction *align_value, bool is_allow_zero)
18331838{
18341839 IrInstructionSliceType *instruction = ir_build_instruction<IrInstructionSliceType>(irb, scope, source_node);
18351840 instruction->is_const = is_const;
18361841 instruction->is_volatile = is_volatile;
18371842 instruction->child_type = child_type;
1843 instruction->sentinel = sentinel;
18381844 instruction->align_value = align_value;
18391845 instruction->is_allow_zero = is_allow_zero;
1840 instruction->is_null_terminated = is_null_terminated;
18411846
1847 if (sentinel != nullptr) ir_ref_instruction(sentinel, irb->current_basic_block);
1848 if (align_value != nullptr) ir_ref_instruction(align_value, irb->current_basic_block);
18421849 ir_ref_instruction(child_type, irb->current_basic_block);
1843 if (align_value) ir_ref_instruction(align_value, irb->current_basic_block);
18441850
18451851 return &instruction->base;
18461852}
......@@ -6067,9 +6073,9 @@ static PtrLen star_token_to_ptr_len(TokenId token_id) {
60676073 case TokenIdStar:
60686074 case TokenIdStarStar:
60696075 return PtrLenSingle;
6070 case TokenIdBracketStarBracket:
6076 case TokenIdLBracket:
60716077 return PtrLenUnknown;
6072 case TokenIdBracketStarCBracket:
6078 case TokenIdSymbol:
60736079 return PtrLenC;
60746080 default:
60756081 zig_unreachable();
......@@ -6080,22 +6086,22 @@ static IrInstruction *ir_gen_pointer_type(IrBuilder *irb, Scope *scope, AstNode
60806086 assert(node->type == NodeTypePointerType);
60816087
60826088 PtrLen ptr_len = star_token_to_ptr_len(node->data.pointer_type.star_token->id);
6083 if (node->data.pointer_type.is_null_terminated) {
6084 if (ptr_len == PtrLenUnknown) {
6085 ptr_len = PtrLenNull;
6086 } else {
6087 exec_add_error_node(irb->codegen, irb->exec, node,
6088 buf_sprintf("null-terminated pointer must be specified with [*] token"));
6089 return irb->codegen->invalid_instruction;
6090 }
6091 }
60926089
60936090 bool is_const = node->data.pointer_type.is_const;
60946091 bool is_volatile = node->data.pointer_type.is_volatile;
60956092 bool is_allow_zero = node->data.pointer_type.allow_zero_token != nullptr;
6093 AstNode *sentinel_expr = node->data.pointer_type.sentinel;
60966094 AstNode *expr_node = node->data.pointer_type.op_expr;
60976095 AstNode *align_expr = node->data.pointer_type.align_expr;
60986096
6097 IrInstruction *sentinel;
6098 if (sentinel_expr != nullptr) {
6099 sentinel = ir_gen_node(irb, sentinel_expr, scope);
6100 if (sentinel == irb->codegen->invalid_instruction)
6101 return sentinel;
6102 } else {
6103 sentinel = nullptr;
6104 }
60996105
61006106 IrInstruction *align_value;
61016107 if (align_expr != nullptr) {
......@@ -6141,7 +6147,7 @@ static IrInstruction *ir_gen_pointer_type(IrBuilder *irb, Scope *scope, AstNode
61416147 }
61426148
61436149 return ir_build_ptr_type(irb, scope, node, child_type, is_const, is_volatile,
6144 ptr_len, align_value, bit_offset_start, host_int_bytes, is_allow_zero);
6150 ptr_len, sentinel, align_value, bit_offset_start, host_int_bytes, is_allow_zero);
61456151}
61466152
61476153static IrInstruction *ir_gen_catch_unreachable(IrBuilder *irb, Scope *scope, AstNode *source_node,
......@@ -6245,13 +6251,22 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
62456251 buf_sprintf("initializing array with struct syntax"));
62466252 return irb->codegen->invalid_instruction;
62476253 }
6254 IrInstruction *sentinel;
6255 if (container_init_expr->type->data.inferred_array_type.sentinel != nullptr) {
6256 sentinel = ir_gen_node(irb, container_init_expr->type->data.inferred_array_type.sentinel, scope);
6257 if (sentinel == irb->codegen->invalid_instruction)
6258 return sentinel;
6259 } else {
6260 sentinel = nullptr;
6261 }
6262
62486263 IrInstruction *elem_type = ir_gen_node(irb,
62496264 container_init_expr->type->data.inferred_array_type.child_type, scope);
62506265 if (elem_type == irb->codegen->invalid_instruction)
62516266 return elem_type;
62526267 size_t item_count = container_init_expr->entries.length;
62536268 IrInstruction *item_count_inst = ir_build_const_usize(irb, scope, node, item_count);
6254 container_type = ir_build_array_type(irb, scope, node, item_count_inst, elem_type, false);
6269 container_type = ir_build_array_type(irb, scope, node, item_count_inst, sentinel, elem_type);
62556270 } else {
62566271 container_type = ir_gen_node(irb, container_init_expr->type, scope);
62576272 if (container_type == irb->codegen->invalid_instruction)
......@@ -6975,10 +6990,20 @@ static IrInstruction *ir_gen_array_type(IrBuilder *irb, Scope *scope, AstNode *n
69756990 bool is_const = node->data.array_type.is_const;
69766991 bool is_volatile = node->data.array_type.is_volatile;
69776992 bool is_allow_zero = node->data.array_type.allow_zero_token != nullptr;
6978 bool is_null_terminated = node->data.array_type.is_null_terminated;
6993 AstNode *sentinel_expr = node->data.array_type.sentinel;
69796994 AstNode *align_expr = node->data.array_type.align_expr;
69806995
69816996 Scope *comptime_scope = create_comptime_scope(irb->codegen, node, scope);
6997
6998 IrInstruction *sentinel;
6999 if (sentinel_expr != nullptr) {
7000 sentinel = ir_gen_node(irb, sentinel_expr, comptime_scope);
7001 if (sentinel == irb->codegen->invalid_instruction)
7002 return sentinel;
7003 } else {
7004 sentinel = nullptr;
7005 }
7006
69827007 if (size_node) {
69837008 if (is_const) {
69847009 add_node_error(irb->codegen, node, buf_create_from_str("const qualifier invalid on array type"));
......@@ -7005,7 +7030,7 @@ static IrInstruction *ir_gen_array_type(IrBuilder *irb, Scope *scope, AstNode *n
70057030 if (child_type == irb->codegen->invalid_instruction)
70067031 return child_type;
70077032
7008 return ir_build_array_type(irb, scope, node, size_value, child_type, is_null_terminated);
7033 return ir_build_array_type(irb, scope, node, size_value, sentinel, child_type);
70097034 } else {
70107035 IrInstruction *align_value;
70117036 if (align_expr != nullptr) {
......@@ -7020,8 +7045,8 @@ static IrInstruction *ir_gen_array_type(IrBuilder *irb, Scope *scope, AstNode *n
70207045 if (child_type == irb->codegen->invalid_instruction)
70217046 return child_type;
70227047
7023 return ir_build_slice_type(irb, scope, node, child_type, is_const, is_volatile, align_value, is_allow_zero,
7024 is_null_terminated);
7048 return ir_build_slice_type(irb, scope, node, child_type, is_const, is_volatile, sentinel,
7049 align_value, is_allow_zero);
70257050 }
70267051}
70277052
......@@ -8698,7 +8723,7 @@ ConstExprValue *const_ptr_pointee(IrAnalyze *ira, CodeGen *codegen, ConstExprVal
86988723 case OnePossibleValueYes:
86998724 return get_the_one_possible_value(codegen, expected_type);
87008725 }
8701 if (!types_have_same_zig_comptime_repr(expected_type, val->type)) {
8726 if (!types_have_same_zig_comptime_repr(codegen, expected_type, val->type)) {
87028727 if ((err = eval_comptime_ptr_reinterpret(ira, codegen, source_node, const_val)))
87038728 return nullptr;
87048729 return const_ptr_pointee_unchecked(codegen, const_val);
......@@ -9846,7 +9871,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
98469871 // alignment can be decreased
98479872 // bit offset attributes must match exactly
98489873 // PtrLenSingle/PtrLenUnknown must match exactly, but PtrLenC matches either one
9849 // PtrLenNull can coerce into PtrLenUnknown
9874 // sentinel-terminated pointers can coerce into PtrLenUnknown
98509875 ZigType *wanted_ptr_type = get_src_ptr_type(wanted_type);
98519876 ZigType *actual_ptr_type = get_src_ptr_type(actual_type);
98529877 bool wanted_allows_zero = ptr_allows_addr_zero(wanted_type);
......@@ -9858,15 +9883,20 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
98589883 bool actual_opt_or_ptr = actual_ptr_type != nullptr &&
98599884 (actual_type->id == ZigTypeIdPointer || actual_type->id == ZigTypeIdOptional);
98609885 if (wanted_opt_or_ptr && actual_opt_or_ptr) {
9861 bool ptr_lens_equal = actual_ptr_type->data.pointer.ptr_len == wanted_ptr_type->data.pointer.ptr_len;
98629886 bool ok_null_term_ptrs =
9863 actual_ptr_type->data.pointer.ptr_len == PtrLenNull ||
9864 wanted_ptr_type->data.pointer.ptr_len == PtrLenUnknown;
9865 if (!(ptr_lens_equal || wanted_is_c_ptr || actual_is_c_ptr || ok_null_term_ptrs)) {
9887 wanted_ptr_type->data.pointer.sentinel == nullptr ||
9888 (actual_ptr_type->data.pointer.sentinel != nullptr &&
9889 const_values_equal(ira->codegen, wanted_ptr_type->data.pointer.sentinel,
9890 actual_ptr_type->data.pointer.sentinel));
9891 if (!ok_null_term_ptrs) {
9892 result.id = ConstCastResultIdPtrSentinel;
9893 result.data.bad_ptr_sentinel = allocate_nonzero<ConstCastPtrSentinel>(1);
9894 result.data.bad_ptr_sentinel->wanted_type = wanted_type;
9895 return result;
9896 }
9897 bool ptr_lens_equal = actual_ptr_type->data.pointer.ptr_len == wanted_ptr_type->data.pointer.ptr_len;
9898 if (!(ptr_lens_equal || wanted_is_c_ptr || actual_is_c_ptr)) {
98669899 result.id = ConstCastResultIdPtrLens;
9867 result.data.bad_ptr_lens = allocate_nonzero<ConstCastBadPtrLens>(1);
9868 result.data.bad_ptr_lens->wanted_type = wanted_type;
9869 result.data.bad_ptr_lens->actual_type = actual_type;
98709900 return result;
98719901 }
98729902
......@@ -9944,14 +9974,15 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
99449974 result.data.array_mismatch->actual_child = actual_type->data.array.child_type;
99459975 return result;
99469976 }
9947 bool ok_null_terminated = !wanted_type->data.array.is_null_terminated ||
9948 actual_type->data.array.is_null_terminated;
9977 bool ok_null_terminated = (wanted_type->data.array.sentinel == nullptr) ||
9978 (actual_type->data.array.sentinel != nullptr &&
9979 const_values_equal(ira->codegen, wanted_type->data.array.sentinel, actual_type->data.array.sentinel));
99499980 if (!ok_null_terminated) {
9950 result.id = ConstCastResultIdBadNullTermArrays;
9951 result.data.bad_null_term_arrays = allocate_nonzero<ConstCastBadNullTermArrays>(1);
9952 result.data.bad_null_term_arrays->child = child;
9953 result.data.bad_null_term_arrays->wanted_type = wanted_type;
9954 result.data.bad_null_term_arrays->actual_type = actual_type;
9981 result.id = ConstCastResultIdSentinelArrays;
9982 result.data.sentinel_arrays = allocate_nonzero<ConstCastBadNullTermArrays>(1);
9983 result.data.sentinel_arrays->child = child;
9984 result.data.sentinel_arrays->wanted_type = wanted_type;
9985 result.data.sentinel_arrays->actual_type = actual_type;
99559986 return result;
99569987 }
99579988 return result;
......@@ -10781,8 +10812,12 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
1078110812 prev_type->data.pointer.child_type->id == ZigTypeIdArray &&
1078210813 (cur_type->data.pointer.is_const || !prev_type->data.pointer.is_const ||
1078310814 prev_type->data.pointer.child_type->data.array.len == 0) &&
10784 (cur_type->data.pointer.child_type->data.array.is_null_terminated ||
10785 !prev_type->data.pointer.child_type->data.array.is_null_terminated) &&
10815 (
10816 prev_type->data.pointer.child_type->data.array.sentinel == nullptr ||
10817 (cur_type->data.pointer.child_type->data.array.sentinel != nullptr &&
10818 const_values_equal(ira->codegen, prev_type->data.pointer.child_type->data.array.sentinel,
10819 cur_type->data.pointer.child_type->data.array.sentinel))
10820 ) &&
1078610821 types_match_const_cast_only(ira,
1078710822 cur_type->data.pointer.child_type->data.array.child_type,
1078810823 prev_type->data.pointer.child_type->data.array.child_type,
......@@ -10798,8 +10833,12 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
1079810833 cur_type->data.pointer.child_type->id == ZigTypeIdArray &&
1079910834 (prev_type->data.pointer.is_const || !cur_type->data.pointer.is_const ||
1080010835 cur_type->data.pointer.child_type->data.array.len == 0) &&
10801 (prev_type->data.pointer.child_type->data.array.is_null_terminated ||
10802 !cur_type->data.pointer.child_type->data.array.is_null_terminated) &&
10836 (
10837 cur_type->data.pointer.child_type->data.array.sentinel == nullptr ||
10838 (prev_type->data.pointer.child_type->data.array.sentinel != nullptr &&
10839 const_values_equal(ira->codegen, cur_type->data.pointer.child_type->data.array.sentinel,
10840 prev_type->data.pointer.child_type->data.array.sentinel))
10841 ) &&
1080310842 types_match_const_cast_only(ira,
1080410843 prev_type->data.pointer.child_type->data.array.child_type,
1080510844 cur_type->data.pointer.child_type->data.array.child_type,
......@@ -10871,11 +10910,12 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
1087110910 } else if (prev_inst->value.type->id == ZigTypeIdPointer) {
1087210911 ZigType *array_type = prev_inst->value.type->data.pointer.child_type;
1087310912 src_assert(array_type->id == ZigTypeIdArray, source_node);
10874 ZigType *ptr_type = get_pointer_to_type_extra(
10913 ZigType *ptr_type = get_pointer_to_type_extra2(
1087510914 ira->codegen, array_type->data.array.child_type,
1087610915 prev_inst->value.type->data.pointer.is_const, false,
10877 array_type->data.array.is_null_terminated ? PtrLenNull : PtrLenUnknown,
10878 0, 0, 0, false);
10916 PtrLenUnknown,
10917 0, 0, 0, false,
10918 VECTOR_INDEX_NONE, nullptr, array_type->data.array.sentinel);
1087910919 ZigType *slice_type = get_slice_type(ira->codegen, ptr_type);
1088010920 if (err_set_type != nullptr) {
1088110921 return get_error_union_type(ira->codegen, err_set_type, slice_type);
......@@ -11682,7 +11722,7 @@ static IrInstruction *ir_analyze_optional_wrap(IrAnalyze *ira, IrInstruction *so
1168211722 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(&ira->new_irb,
1168311723 source_instr->scope, source_instr->source_node);
1168411724 const_instruction->base.value.special = ConstValSpecialStatic;
11685 if (types_have_same_zig_comptime_repr(wanted_type, payload_type)) {
11725 if (types_have_same_zig_comptime_repr(ira->codegen, wanted_type, payload_type)) {
1168611726 copy_const_val(&const_instruction->base.value, val, val->data.x_ptr.mut == ConstPtrMutComptimeConst);
1168711727 } else {
1168811728 const_instruction->base.value.data.x_optional = val;
......@@ -12601,14 +12641,24 @@ static void report_recursive_error(IrAnalyze *ira, AstNode *source_node, ConstCa
1260112641 break;
1260212642 }
1260312643 case ConstCastResultIdPtrLens: {
12604 ZigType *wanted_type = cast_result->data.bad_ptr_lens->wanted_type;
12605 ZigType *actual_type = cast_result->data.bad_ptr_lens->actual_type;
12606 bool wanted_null_term = wanted_type->data.pointer.ptr_len == PtrLenNull;
12607 bool actual_null_term = actual_type->data.pointer.ptr_len == PtrLenNull;
12608 if (wanted_null_term && !actual_null_term) {
12609 add_error_note(ira->codegen, parent_msg, source_node,
12610 buf_sprintf("destination type requires null termination"));
12611 }
12644 add_error_note(ira->codegen, parent_msg, source_node,
12645 buf_sprintf("pointer length mismatch"));
12646 break;
12647 }
12648 case ConstCastResultIdPtrSentinel: {
12649 ZigType *wanted_type = cast_result->data.bad_ptr_sentinel->wanted_type;
12650 Buf *msg = buf_sprintf("destination pointer requires a terminating '");
12651 render_const_value(ira->codegen, msg, wanted_type->data.pointer.sentinel);
12652 buf_appendf(msg, "' sentinel value");
12653 add_error_note(ira->codegen, parent_msg, source_node, msg);
12654 break;
12655 }
12656 case ConstCastResultIdSentinelArrays: {
12657 ZigType *wanted_type = cast_result->data.sentinel_arrays->wanted_type;
12658 Buf *msg = buf_sprintf("destination array requires a terminating '");
12659 render_const_value(ira->codegen, msg, wanted_type->data.pointer.sentinel);
12660 buf_appendf(msg, "' sentinel value");
12661 add_error_note(ira->codegen, parent_msg, source_node, msg);
1261212662 break;
1261312663 }
1261412664 case ConstCastResultIdCV: {
......@@ -12642,7 +12692,6 @@ static void report_recursive_error(IrAnalyze *ira, AstNode *source_node, ConstCa
1264212692 case ConstCastResultIdUnresolvedInferredErrSet: // TODO
1264312693 case ConstCastResultIdAsyncAllocatorType: // TODO
1264412694 case ConstCastResultIdArrayChild: // TODO
12645 case ConstCastResultIdBadNullTermArrays: // TODO
1264612695 break;
1264712696 }
1264812697}
......@@ -13013,16 +13062,18 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1301313062
1301413063 // *[N]T to [*]T and [*c]T
1301513064 if (wanted_type->id == ZigTypeIdPointer &&
13016 (wanted_type->data.pointer.ptr_len == PtrLenUnknown || wanted_type->data.pointer.ptr_len == PtrLenC ||
13017 wanted_type->data.pointer.ptr_len == PtrLenNull) &&
13065 (wanted_type->data.pointer.ptr_len == PtrLenUnknown || wanted_type->data.pointer.ptr_len == PtrLenC) &&
1301813066 actual_type->id == ZigTypeIdPointer &&
1301913067 actual_type->data.pointer.ptr_len == PtrLenSingle &&
1302013068 actual_type->data.pointer.child_type->id == ZigTypeIdArray &&
1302113069 (!actual_type->data.pointer.is_const || wanted_type->data.pointer.is_const) &&
1302213070 (!actual_type->data.pointer.is_volatile || wanted_type->data.pointer.is_volatile))
1302313071 {
13024 if (wanted_type->data.pointer.ptr_len != PtrLenNull ||
13025 actual_type->data.pointer.child_type->data.array.is_null_terminated)
13072 ZigType *actual_array_type = actual_type->data.pointer.child_type;
13073 if (wanted_type->data.pointer.sentinel == nullptr ||
13074 (actual_array_type->data.array.sentinel != nullptr &&
13075 const_values_equal(ira->codegen, wanted_type->data.pointer.sentinel,
13076 actual_array_type->data.array.sentinel)))
1302613077 {
1302713078 if ((err = type_resolve(ira->codegen, actual_type->data.pointer.child_type, ResolveStatusAlignmentKnown)))
1302813079 return ira->codegen->invalid_instruction;
......@@ -14741,7 +14792,6 @@ static bool is_pointer_arithmetic_allowed(ZigType *lhs_type, IrBinOp op) {
1474114792 case PtrLenSingle:
1474214793 return lhs_type->data.pointer.child_type->id == ZigTypeIdArray;
1474314794 case PtrLenUnknown:
14744 case PtrLenNull:
1474514795 case PtrLenC:
1474614796 return true;
1474714797 }
......@@ -15007,7 +15057,7 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i
1500715057 if (!op2_val)
1500815058 return ira->codegen->invalid_instruction;
1500915059
15010 bool is_null_terminated = false;
15060 ConstExprValue *sentinel1 = nullptr;
1501115061 ConstExprValue *op1_array_val;
1501215062 size_t op1_array_index;
1501315063 size_t op1_array_end;
......@@ -15017,16 +15067,17 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i
1501715067 op1_array_val = op1_val;
1501815068 op1_array_index = 0;
1501915069 op1_array_end = op1_type->data.array.len;
15070 sentinel1 = op1_type->data.array.sentinel;
1502015071 } else if (op1_type->id == ZigTypeIdPointer &&
1502115072 op1_type->data.pointer.child_type == ira->codegen->builtin_types.entry_u8 &&
15022 op1_type->data.pointer.ptr_len == PtrLenNull &&
15073 op1_type->data.pointer.sentinel != nullptr &&
1502315074 op1_val->data.x_ptr.special == ConstPtrSpecialBaseArray)
1502415075 {
1502515076 child_type = op1_type->data.pointer.child_type;
1502615077 op1_array_val = op1_val->data.x_ptr.data.base_array.array_val;
1502715078 op1_array_index = op1_val->data.x_ptr.data.base_array.elem_index;
1502815079 op1_array_end = op1_array_val->type->data.array.len;
15029 is_null_terminated = true;
15080 sentinel1 = op1_type->data.pointer.sentinel;
1503015081 } else if (is_slice(op1_type)) {
1503115082 ZigType *ptr_type = op1_type->data.structure.fields[slice_ptr_index]->type_entry;
1503215083 child_type = ptr_type->data.pointer.child_type;
......@@ -15036,6 +15087,7 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i
1503615087 op1_array_index = ptr_val->data.x_ptr.data.base_array.elem_index;
1503715088 ConstExprValue *len_val = op1_val->data.x_struct.fields[slice_len_index];
1503815089 op1_array_end = op1_array_index + bigint_as_usize(&len_val->data.x_bigint);
15090 sentinel1 = ptr_type->data.pointer.sentinel;
1503915091 } else if (op1_type->id == ZigTypeIdPointer && op1_type->data.pointer.ptr_len == PtrLenSingle &&
1504015092 op1_type->data.pointer.child_type->id == ZigTypeIdArray)
1504115093 {
......@@ -15046,13 +15098,14 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i
1504615098 return ira->codegen->invalid_instruction;
1504715099 op1_array_index = 0;
1504815100 op1_array_end = array_type->data.array.len;
15049 is_null_terminated = is_null_terminated || array_type->data.array.is_null_terminated;
15101 sentinel1 = array_type->data.array.sentinel;
1505015102 } else {
1505115103 ir_add_error(ira, op1,
1505215104 buf_sprintf("expected array, found '%s'", buf_ptr(&op1->value.type->name)));
1505315105 return ira->codegen->invalid_instruction;
1505415106 }
1505515107
15108 ConstExprValue *sentinel2 = nullptr;
1505615109 ConstExprValue *op2_array_val;
1505715110 size_t op2_array_index;
1505815111 size_t op2_array_end;
......@@ -15062,15 +15115,17 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i
1506215115 op2_array_val = op2_val;
1506315116 op2_array_index = 0;
1506415117 op2_array_end = op2_array_val->type->data.array.len;
15118 sentinel2 = op2_type->data.array.sentinel;
1506515119 } else if (op2_type->id == ZigTypeIdPointer &&
15066 op2_type->data.pointer.ptr_len == PtrLenNull &&
15120 op2_type->data.pointer.sentinel != nullptr &&
1506715121 op2_val->data.x_ptr.special == ConstPtrSpecialBaseArray)
1506815122 {
1506915123 op2_type_valid = op2_type->data.pointer.child_type == child_type;
1507015124 op2_array_val = op2_val->data.x_ptr.data.base_array.array_val;
1507115125 op2_array_index = op2_val->data.x_ptr.data.base_array.elem_index;
1507215126 op2_array_end = op2_array_val->type->data.array.len;
15073 is_null_terminated = true;
15127
15128 sentinel2 = op2_type->data.pointer.sentinel;
1507415129 } else if (is_slice(op2_type)) {
1507515130 ZigType *ptr_type = op2_type->data.structure.fields[slice_ptr_index]->type_entry;
1507615131 op2_type_valid = ptr_type->data.pointer.child_type == child_type;
......@@ -15080,6 +15135,8 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i
1508015135 op2_array_index = ptr_val->data.x_ptr.data.base_array.elem_index;
1508115136 ConstExprValue *len_val = op2_val->data.x_struct.fields[slice_len_index];
1508215137 op2_array_end = op2_array_index + bigint_as_usize(&len_val->data.x_bigint);
15138
15139 sentinel2 = ptr_type->data.pointer.sentinel;
1508315140 } else if (op2_type->id == ZigTypeIdPointer && op2_type->data.pointer.ptr_len == PtrLenSingle &&
1508415141 op2_type->data.pointer.child_type->id == ZigTypeIdArray)
1508515142 {
......@@ -15090,7 +15147,8 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i
1509015147 return ira->codegen->invalid_instruction;
1509115148 op2_array_index = 0;
1509215149 op2_array_end = array_type->data.array.len;
15093 is_null_terminated = is_null_terminated || array_type->data.array.is_null_terminated;
15150
15151 sentinel2 = array_type->data.array.sentinel;
1509415152 } else {
1509515153 ir_add_error(ira, op2,
1509615154 buf_sprintf("expected array or C string literal, found '%s'", buf_ptr(&op2->value.type->name)));
......@@ -15103,6 +15161,19 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i
1510315161 return ira->codegen->invalid_instruction;
1510415162 }
1510515163
15164 ConstExprValue *sentinel;
15165 if (sentinel1 != nullptr && sentinel2 != nullptr) {
15166 // When there is a sentinel mismatch, no sentinel on the result. The type system
15167 // will catch this if it is a problem.
15168 sentinel = const_values_equal(ira->codegen, sentinel1, sentinel2) ? sentinel1 : nullptr;
15169 } else if (sentinel1 != nullptr) {
15170 sentinel = sentinel1;
15171 } else if (sentinel2 != nullptr) {
15172 sentinel = sentinel2;
15173 } else {
15174 sentinel = nullptr;
15175 }
15176
1510615177 // The type of result is populated in the following if blocks
1510715178 IrInstruction *result = ir_const(ira, &instruction->base, nullptr);
1510815179 ConstExprValue *out_val = &result->value;
......@@ -15110,24 +15181,25 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i
1511015181 ConstExprValue *out_array_val;
1511115182 size_t new_len = (op1_array_end - op1_array_index) + (op2_array_end - op2_array_index);
1511215183 if (op1_type->id == ZigTypeIdArray || op2_type->id == ZigTypeIdArray) {
15113 result->value.type = get_array_type(ira->codegen, child_type, new_len, false);
15184 result->value.type = get_array_type(ira->codegen, child_type, new_len, sentinel);
1511415185
1511515186 out_array_val = out_val;
1511615187 } else if (op1_type->id == ZigTypeIdPointer || op2_type->id == ZigTypeIdPointer) {
1511715188 out_array_val = create_const_vals(1);
1511815189 out_array_val->special = ConstValSpecialStatic;
15119 out_array_val->type = get_array_type(ira->codegen, child_type, new_len, is_null_terminated);
15190 out_array_val->type = get_array_type(ira->codegen, child_type, new_len, sentinel);
1512015191
1512115192 out_val->data.x_ptr.special = ConstPtrSpecialRef;
1512215193 out_val->data.x_ptr.data.ref.pointee = out_array_val;
1512315194 out_val->type = get_pointer_to_type(ira->codegen, out_array_val->type, true);
1512415195 } else if (is_slice(op1_type) || is_slice(op2_type)) {
15125 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, child_type,
15126 true, false, PtrLenUnknown, 0, 0, 0, false);
15196 ZigType *ptr_type = get_pointer_to_type_extra2(ira->codegen, child_type,
15197 true, false, PtrLenUnknown, 0, 0, 0, false,
15198 VECTOR_INDEX_NONE, nullptr, sentinel);
1512715199 result->value.type = get_slice_type(ira->codegen, ptr_type);
1512815200 out_array_val = create_const_vals(1);
1512915201 out_array_val->special = ConstValSpecialStatic;
15130 out_array_val->type = get_array_type(ira->codegen, child_type, new_len, false);
15202 out_array_val->type = get_array_type(ira->codegen, child_type, new_len, sentinel);
1513115203
1513215204 out_val->data.x_struct.fields = alloc_const_vals_ptrs(2);
1513315205
......@@ -15141,12 +15213,12 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i
1514115213 out_val->data.x_struct.fields[slice_len_index]->special = ConstValSpecialStatic;
1514215214 bigint_init_unsigned(&out_val->data.x_struct.fields[slice_len_index]->data.x_bigint, new_len);
1514315215 } else {
15144 result->value.type = get_pointer_to_type_extra(ira->codegen, child_type, true, false, PtrLenNull,
15145 0, 0, 0, false);
15216 result->value.type = get_pointer_to_type_extra2(ira->codegen, child_type, true, false, PtrLenUnknown,
15217 0, 0, 0, false, VECTOR_INDEX_NONE, nullptr, sentinel);
1514615218
1514715219 out_array_val = create_const_vals(1);
1514815220 out_array_val->special = ConstValSpecialStatic;
15149 out_array_val->type = get_array_type(ira->codegen, child_type, new_len, false);
15221 out_array_val->type = get_array_type(ira->codegen, child_type, new_len, sentinel);
1515015222 out_val->data.x_ptr.special = ConstPtrSpecialBaseArray;
1515115223 out_val->data.x_ptr.data.base_array.array_val = out_array_val;
1515215224 out_val->data.x_ptr.data.base_array.elem_index = 0;
......@@ -15159,26 +15231,36 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i
1515915231 return result;
1516015232 }
1516115233
15162 out_array_val->data.x_array.data.s_none.elements = create_const_vals(new_len);
15234 uint64_t full_len = new_len + ((sentinel != nullptr) ? 1 : 0);
15235 out_array_val->data.x_array.data.s_none.elements = create_const_vals(full_len);
1516315236 // TODO handle the buf case here for an optimization
1516415237 expand_undef_array(ira->codegen, op1_array_val);
1516515238 expand_undef_array(ira->codegen, op2_array_val);
1516615239
1516715240 size_t next_index = 0;
1516815241 for (size_t i = op1_array_index; i < op1_array_end; i += 1, next_index += 1) {
15169 copy_const_val(&out_array_val->data.x_array.data.s_none.elements[next_index],
15170 &op1_array_val->data.x_array.data.s_none.elements[i], true);
15242 ConstExprValue *elem_dest_val = &out_array_val->data.x_array.data.s_none.elements[next_index];
15243 copy_const_val(elem_dest_val, &op1_array_val->data.x_array.data.s_none.elements[i], false);
15244 elem_dest_val->parent.id = ConstParentIdArray;
15245 elem_dest_val->parent.data.p_array.array_val = out_array_val;
15246 elem_dest_val->parent.data.p_array.elem_index = next_index;
1517115247 }
1517215248 for (size_t i = op2_array_index; i < op2_array_end; i += 1, next_index += 1) {
15173 copy_const_val(&out_array_val->data.x_array.data.s_none.elements[next_index],
15174 &op2_array_val->data.x_array.data.s_none.elements[i], true);
15175 }
15176 if (next_index < new_len) {
15177 ConstExprValue *null_byte = &out_array_val->data.x_array.data.s_none.elements[next_index];
15178 init_const_unsigned_negative(null_byte, child_type, 0, false);
15249 ConstExprValue *elem_dest_val = &out_array_val->data.x_array.data.s_none.elements[next_index];
15250 copy_const_val(elem_dest_val, &op2_array_val->data.x_array.data.s_none.elements[i], false);
15251 elem_dest_val->parent.id = ConstParentIdArray;
15252 elem_dest_val->parent.data.p_array.array_val = out_array_val;
15253 elem_dest_val->parent.data.p_array.elem_index = next_index;
15254 }
15255 if (next_index < full_len) {
15256 ConstExprValue *elem_dest_val = &out_array_val->data.x_array.data.s_none.elements[next_index];
15257 copy_const_val(elem_dest_val, sentinel, false);
15258 elem_dest_val->parent.id = ConstParentIdArray;
15259 elem_dest_val->parent.data.p_array.array_val = out_array_val;
15260 elem_dest_val->parent.data.p_array.elem_index = next_index;
1517915261 next_index += 1;
1518015262 }
15181 assert(next_index == new_len);
15263 assert(next_index == full_len);
1518215264
1518315265 return result;
1518415266}
......@@ -15230,7 +15312,7 @@ static IrInstruction *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp *
1523015312
1523115313 ZigType *child_type = array_type->data.array.child_type;
1523215314 ZigType *result_array_type = get_array_type(ira->codegen, child_type, new_array_len,
15233 array_type->data.array.is_null_terminated);
15315 array_type->data.array.sentinel);
1523415316
1523515317 IrInstruction *array_result;
1523615318 if (array_val->special == ConstValSpecialUndef || array_val->data.x_array.special == ConstArraySpecialUndef) {
......@@ -15250,7 +15332,7 @@ static IrInstruction *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp *
1525015332
1525115333 // TODO optimize the buf case
1525215334 expand_undef_array(ira->codegen, array_val);
15253 size_t extra_null_term = array_type->data.array.is_null_terminated ? 1 : 0;
15335 size_t extra_null_term = (array_type->data.array.sentinel != nullptr) ? 1 : 0;
1525415336 out_val->data.x_array.data.s_none.elements = create_const_vals(new_array_len + extra_null_term);
1525515337
1525615338 uint64_t i = 0;
......@@ -15266,10 +15348,9 @@ static IrInstruction *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp *
1526615348 }
1526715349 assert(i == new_array_len);
1526815350
15269 if (array_type->data.array.is_null_terminated) {
15270 ConstExprValue *null_value = get_null_value(array_type->data.array.child_type);
15351 if (array_type->data.array.sentinel != nullptr) {
1527115352 ConstExprValue *elem_dest_val = &out_val->data.x_array.data.s_none.elements[i];
15272 copy_const_val(elem_dest_val, null_value, false);
15353 copy_const_val(elem_dest_val, array_type->data.array.sentinel, false);
1527315354 elem_dest_val->parent.id = ConstParentIdArray;
1527415355 elem_dest_val->parent.data.p_array.array_val = out_val;
1527515356 elem_dest_val->parent.data.p_array.elem_index = i;
......@@ -17565,7 +17646,7 @@ static Error ir_read_const_ptr(IrAnalyze *ira, CodeGen *codegen, AstNode *source
1756517646 size_t dst_size = type_size(codegen, out_val->type);
1756617647
1756717648 if (dst_size <= src_size) {
17568 if (src_size == dst_size && types_have_same_zig_comptime_repr(out_val->type, pointee->type)) {
17649 if (src_size == dst_size && types_have_same_zig_comptime_repr(codegen, out_val->type, pointee->type)) {
1756917650 copy_const_val(out_val, pointee, ptr_val->data.x_ptr.mut != ConstPtrMutComptimeVar);
1757017651 return ErrorNone;
1757117652 }
......@@ -18316,11 +18397,11 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1831618397 uint64_t index = bigint_as_u64(&casted_elem_index->value.data.x_bigint);
1831718398 if (array_type->id == ZigTypeIdArray) {
1831818399 uint64_t array_len = array_type->data.array.len;
18319 if (index == array_len && array_type->data.array.is_null_terminated) {
18400 if (index == array_len && array_type->data.array.sentinel != nullptr) {
1832018401 ZigType *elem_type = array_type->data.array.child_type;
18321 IrInstruction *null_element = ir_const(ira, &elem_ptr_instruction->base, elem_type);
18322 null_element->value = *get_null_value(elem_type);
18323 return ir_get_ref(ira, &elem_ptr_instruction->base, null_element, true, false);
18402 IrInstruction *sentinel_elem = ir_const(ira, &elem_ptr_instruction->base, elem_type);
18403 copy_const_val(&sentinel_elem->value, array_type->data.array.sentinel, false);
18404 return ir_get_ref(ira, &elem_ptr_instruction->base, sentinel_elem, true, false);
1832418405 }
1832518406 if (index >= array_len) {
1832618407 ir_add_error_node(ira, elem_ptr_instruction->base.source_node,
......@@ -18337,7 +18418,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1833718418 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile,
1833818419 elem_ptr_instruction->ptr_len,
1833918420 get_ptr_align(ira->codegen, ptr_type), 0, host_vec_len, false, (uint32_t)index,
18340 nullptr);
18421 nullptr, nullptr);
1834118422 } else if (return_type->data.pointer.explicit_alignment != 0) {
1834218423 // figure out the largest alignment possible
1834318424
......@@ -18457,7 +18538,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1845718538 new_index = offset + index;
1845818539 ZigType *array_type = array_ptr_val->data.x_ptr.data.base_array.array_val->type;
1845918540 mem_size = array_type->data.array.len;
18460 if (array_type->data.array.is_null_terminated) {
18541 if (array_type->data.array.sentinel != nullptr) {
1846118542 mem_size += 1;
1846218543 }
1846318544 old_size = mem_size - offset;
......@@ -18579,7 +18660,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1857918660 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile,
1858018661 elem_ptr_instruction->ptr_len,
1858118662 get_ptr_align(ira->codegen, ptr_type), 0, host_vec_len, false, VECTOR_INDEX_RUNTIME,
18582 nullptr);
18663 nullptr, nullptr);
1858318664 } else {
1858418665 // runtime known element index
1858518666 switch (type_requires_comptime(ira->codegen, return_type)) {
......@@ -18783,7 +18864,7 @@ static IrInstruction *ir_analyze_inferred_field_ptr(IrAnalyze *ira, Buf *field_n
1878318864 ZigType *elem_type = ira->codegen->builtin_types.entry_var;
1878418865 ZigType *field_ptr_type = get_pointer_to_type_extra2(ira->codegen, elem_type,
1878518866 container_ptr_type->data.pointer.is_const, container_ptr_type->data.pointer.is_volatile,
18786 PtrLenSingle, 0, 0, 0, false, VECTOR_INDEX_NONE, inferred_struct_field);
18867 PtrLenSingle, 0, 0, 0, false, VECTOR_INDEX_NONE, inferred_struct_field, nullptr);
1878718868
1878818869 if (instr_is_comptime(container_ptr)) {
1878918870 IrInstruction *result = ir_const(ira, source_instr, field_ptr_type);
......@@ -19565,6 +19646,12 @@ static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1956519646 return ira->codegen->invalid_instruction;
1956619647 }
1956719648
19649 if (slice_type_instruction->sentinel != nullptr) {
19650 lazy_slice_type->sentinel = slice_type_instruction->sentinel->child;
19651 if (ir_resolve_const(ira, lazy_slice_type->sentinel, LazyOk) == nullptr)
19652 return ira->codegen->invalid_instruction;
19653 }
19654
1956819655 lazy_slice_type->elem_type = slice_type_instruction->child_type->child;
1956919656 if (ir_resolve_type_lazy(ira, lazy_slice_type->elem_type) == nullptr)
1957019657 return ira->codegen->invalid_instruction;
......@@ -19572,7 +19659,6 @@ static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1957219659 lazy_slice_type->is_const = slice_type_instruction->is_const;
1957319660 lazy_slice_type->is_volatile = slice_type_instruction->is_volatile;
1957419661 lazy_slice_type->is_allowzero = slice_type_instruction->is_allow_zero;
19575 lazy_slice_type->is_null_terminated = slice_type_instruction->is_null_terminated;
1957619662
1957719663 return result;
1957819664}
......@@ -19647,6 +19733,22 @@ static IrInstruction *ir_analyze_instruction_array_type(IrAnalyze *ira,
1964719733 ZigType *child_type = ir_resolve_type(ira, child_type_value);
1964819734 if (type_is_invalid(child_type))
1964919735 return ira->codegen->invalid_instruction;
19736
19737 ConstExprValue *sentinel_val;
19738 if (array_type_instruction->sentinel != nullptr) {
19739 IrInstruction *uncasted_sentinel = array_type_instruction->sentinel->child;
19740 if (type_is_invalid(uncasted_sentinel->value.type))
19741 return ira->codegen->invalid_instruction;
19742 IrInstruction *sentinel = ir_implicit_cast(ira, uncasted_sentinel, child_type);
19743 if (type_is_invalid(sentinel->value.type))
19744 return ira->codegen->invalid_instruction;
19745 sentinel_val = ir_resolve_const(ira, sentinel, UndefBad);
19746 if (sentinel_val == nullptr)
19747 return ira->codegen->invalid_instruction;
19748 } else {
19749 sentinel_val = nullptr;
19750 }
19751
1965019752 switch (child_type->id) {
1965119753 case ZigTypeIdInvalid: // handled above
1965219754 zig_unreachable();
......@@ -19682,8 +19784,7 @@ static IrInstruction *ir_analyze_instruction_array_type(IrAnalyze *ira,
1968219784 {
1968319785 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusSizeKnown)))
1968419786 return ira->codegen->invalid_instruction;
19685 ZigType *result_type = get_array_type(ira->codegen, child_type, size,
19686 array_type_instruction->is_null_terminated);
19787 ZigType *result_type = get_array_type(ira->codegen, child_type, size, sentinel_val);
1968719788 return ir_const_type(ira, &array_type_instruction->base, result_type);
1968819789 }
1968919790 }
......@@ -19802,7 +19903,7 @@ static IrInstruction *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInstr
1980219903 ZigType *result_type = get_pointer_to_type_extra(ira->codegen, child_type,
1980319904 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile, PtrLenSingle, 0, 0, 0, false);
1980419905
19805 bool same_comptime_repr = types_have_same_zig_comptime_repr(child_type, type_entry);
19906 bool same_comptime_repr = types_have_same_zig_comptime_repr(ira->codegen, child_type, type_entry);
1980619907
1980719908 if (instr_is_comptime(base_ptr)) {
1980819909 ConstExprValue *ptr_val = ir_resolve_const(ira, base_ptr, UndefBad);
......@@ -20759,7 +20860,7 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
2075920860 if (container_type->id == ZigTypeIdArray) {
2076020861 ZigType *child_type = container_type->data.array.child_type;
2076120862 if (container_type->data.array.len != elem_count) {
20762 ZigType *literal_type = get_array_type(ira->codegen, child_type, elem_count, false);
20863 ZigType *literal_type = get_array_type(ira->codegen, child_type, elem_count, nullptr);
2076320864
2076420865 ir_add_error(ira, &instruction->base,
2076520866 buf_sprintf("expected %s literal, found %s literal",
......@@ -21246,7 +21347,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr
2124621347
2124721348 ConstExprValue *declaration_array = create_const_vals(1);
2124821349 declaration_array->special = ConstValSpecialStatic;
21249 declaration_array->type = get_array_type(ira->codegen, type_info_declaration_type, declaration_count, false);
21350 declaration_array->type = get_array_type(ira->codegen, type_info_declaration_type, declaration_count, nullptr);
2125021351 declaration_array->data.x_array.special = ConstArraySpecialNone;
2125121352 declaration_array->data.x_array.data.s_none.elements = create_const_vals(declaration_count);
2125221353 init_const_slice(ira->codegen, out_val, declaration_array, 0, declaration_count, false);
......@@ -21391,7 +21492,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr
2139121492 ConstExprValue *fn_arg_name_array = create_const_vals(1);
2139221493 fn_arg_name_array->special = ConstValSpecialStatic;
2139321494 fn_arg_name_array->type = get_array_type(ira->codegen,
21394 get_slice_type(ira->codegen, u8_ptr), fn_arg_count, false);
21495 get_slice_type(ira->codegen, u8_ptr), fn_arg_count, nullptr);
2139521496 fn_arg_name_array->data.x_array.special = ConstArraySpecialNone;
2139621497 fn_arg_name_array->data.x_array.data.s_none.elements = create_const_vals(fn_arg_count);
2139721498
......@@ -21446,7 +21547,6 @@ static BuiltinPtrSize ptr_len_to_size_enum_index(PtrLen ptr_len) {
2144621547 case PtrLenSingle:
2144721548 return BuiltinPtrSizeOne;
2144821549 case PtrLenUnknown:
21449 case PtrLenNull:
2145021550 return BuiltinPtrSizeMany;
2145121551 case PtrLenC:
2145221552 return BuiltinPtrSizeC;
......@@ -21527,11 +21627,21 @@ static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_ty
2152721627 fields[5]->special = ConstValSpecialStatic;
2152821628 fields[5]->type = ira->codegen->builtin_types.entry_bool;
2152921629 fields[5]->data.x_bool = attrs_type->data.pointer.allow_zero;
21530 // is_null_terminated: bool
21531 ensure_field_index(result->type, "is_null_terminated", 6);
21630 // sentinel: ?*const c_void
21631 ZigType *ptr_type = get_pointer_to_type(ira->codegen, ira->codegen->builtin_types.entry_c_void, true);
21632 ensure_field_index(result->type, "sentinel", 6);
2153221633 fields[6]->special = ConstValSpecialStatic;
21533 fields[6]->type = ira->codegen->builtin_types.entry_bool;
21534 fields[6]->data.x_bool = attrs_type->data.pointer.ptr_len == PtrLenNull;
21634 fields[6]->type = get_optional_type(ira->codegen, ptr_type);
21635 if (attrs_type->data.pointer.sentinel == nullptr) {
21636 fields[6]->data.x_optional = nullptr;
21637 } else {
21638 ConstExprValue *ptr_val = create_const_vals(1);
21639 fields[6]->data.x_optional = ptr_val;
21640 ptr_val->data.x_ptr.special = ConstPtrSpecialRef;
21641 ptr_val->data.x_ptr.mut = ConstPtrMutComptimeConst;
21642 ptr_val->data.x_ptr.data.ref.pointee = create_const_vals(1);
21643 copy_const_val(ptr_val->data.x_ptr.data.ref.pointee, attrs_type->data.pointer.sentinel, false);
21644 }
2153521645
2153621646 return result;
2153721647};
......@@ -21652,11 +21762,22 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
2165221762 fields[1]->special = ConstValSpecialStatic;
2165321763 fields[1]->type = ira->codegen->builtin_types.entry_type;
2165421764 fields[1]->data.x_type = type_entry->data.array.child_type;
21655 // is_null_terminated: bool
21656 ensure_field_index(result->type, "is_null_terminated", 2);
21765 // sentinel: ?*const c_void
2165721766 fields[2]->special = ConstValSpecialStatic;
21658 fields[2]->type = ira->codegen->builtin_types.entry_bool;
21659 fields[2]->data.x_bool = type_entry->data.array.is_null_terminated;
21767 ZigType *ptr_type = get_pointer_to_type(ira->codegen,
21768 ira->codegen->builtin_types.entry_c_void, true);
21769 fields[2]->type = get_optional_type(ira->codegen, ptr_type);
21770 if (type_entry->data.array.sentinel == nullptr) {
21771 fields[2]->data.x_optional = nullptr;
21772 } else {
21773 ConstExprValue *ptr_val = create_const_vals(1);
21774 fields[2]->data.x_optional = ptr_val;
21775 ptr_val->type = ptr_type;
21776 ptr_val->data.x_ptr.special = ConstPtrSpecialRef;
21777 ptr_val->data.x_ptr.mut = ConstPtrMutComptimeConst;
21778 ptr_val->data.x_ptr.data.ref.pointee = create_const_vals(1);
21779 copy_const_val(ptr_val->data.x_ptr.data.ref.pointee, type_entry->data.array.sentinel, false);
21780 }
2166021781
2166121782 break;
2166221783 }
......@@ -21744,7 +21865,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
2174421865
2174521866 ConstExprValue *enum_field_array = create_const_vals(1);
2174621867 enum_field_array->special = ConstValSpecialStatic;
21747 enum_field_array->type = get_array_type(ira->codegen, type_info_enum_field_type, enum_field_count, false);
21868 enum_field_array->type = get_array_type(ira->codegen, type_info_enum_field_type, enum_field_count, nullptr);
2174821869 enum_field_array->data.x_array.special = ConstArraySpecialNone;
2174921870 enum_field_array->data.x_array.data.s_none.elements = create_const_vals(enum_field_count);
2175021871
......@@ -21792,7 +21913,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
2179221913 uint32_t error_count = type_entry->data.error_set.err_count;
2179321914 ConstExprValue *error_array = create_const_vals(1);
2179421915 error_array->special = ConstValSpecialStatic;
21795 error_array->type = get_array_type(ira->codegen, type_info_error_type, error_count, false);
21916 error_array->type = get_array_type(ira->codegen, type_info_error_type, error_count, nullptr);
2179621917 error_array->data.x_array.special = ConstArraySpecialNone;
2179721918 error_array->data.x_array.data.s_none.elements = create_const_vals(error_count);
2179821919
......@@ -21888,7 +22009,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
2188822009
2188922010 ConstExprValue *union_field_array = create_const_vals(1);
2189022011 union_field_array->special = ConstValSpecialStatic;
21891 union_field_array->type = get_array_type(ira->codegen, type_info_union_field_type, union_field_count, false);
22012 union_field_array->type = get_array_type(ira->codegen, type_info_union_field_type, union_field_count, nullptr);
2189222013 union_field_array->data.x_array.special = ConstArraySpecialNone;
2189322014 union_field_array->data.x_array.data.s_none.elements = create_const_vals(union_field_count);
2189422015
......@@ -21968,7 +22089,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
2196822089
2196922090 ConstExprValue *struct_field_array = create_const_vals(1);
2197022091 struct_field_array->special = ConstValSpecialStatic;
21971 struct_field_array->type = get_array_type(ira->codegen, type_info_struct_field_type, struct_field_count, false);
22092 struct_field_array->type = get_array_type(ira->codegen, type_info_struct_field_type, struct_field_count, nullptr);
2197222093 struct_field_array->data.x_array.special = ConstArraySpecialNone;
2197322094 struct_field_array->data.x_array.data.s_none.elements = create_const_vals(struct_field_count);
2197422095
......@@ -22071,7 +22192,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
2207122192
2207222193 ConstExprValue *fn_arg_array = create_const_vals(1);
2207322194 fn_arg_array->special = ConstValSpecialStatic;
22074 fn_arg_array->type = get_array_type(ira->codegen, type_info_fn_arg_type, fn_arg_count, false);
22195 fn_arg_array->type = get_array_type(ira->codegen, type_info_fn_arg_type, fn_arg_count, nullptr);
2207522196 fn_arg_array->data.x_array.special = ConstArraySpecialNone;
2207622197 fn_arg_array->data.x_array.data.s_none.elements = create_const_vals(fn_arg_count);
2207722198
......@@ -22169,6 +22290,17 @@ static ConstExprValue *get_const_field(IrAnalyze *ira, ConstExprValue *struct_va
2216922290 return struct_value->data.x_struct.fields[field_index];
2217022291}
2217122292
22293static ConstExprValue *get_const_field_variant(IrAnalyze *ira, ConstExprValue *struct_value,
22294 const char *name, size_t field_index)
22295{
22296 ConstExprValue *field_val = get_const_field(ira, struct_value, name, field_index);
22297 assert(field_val->type->id == ZigTypeIdOptional);
22298 ConstExprValue *opt_val = field_val->data.x_optional;
22299 if (opt_val == nullptr) return nullptr;
22300 assert(opt_val->type->id == ZigTypeIdPointer);
22301 return const_ptr_pointee_unchecked(ira->codegen, opt_val);
22302}
22303
2217222304static bool get_const_field_bool(IrAnalyze *ira, ConstExprValue *struct_value, const char *name, size_t field_index)
2217322305{
2217422306 ConstExprValue *value = get_const_field(ira, struct_value, name, field_index);
......@@ -22232,7 +22364,7 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInstruction *instruction, Zi
2223222364 assert(size_value->type == ir_type_info_get_type(ira, "Size", type_info_pointer_type));
2223322365 BuiltinPtrSize size_enum_index = (BuiltinPtrSize)bigint_as_u32(&size_value->data.x_enum_tag);
2223422366 PtrLen ptr_len = size_enum_index_to_ptr_len(size_enum_index);
22235 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen,
22367 ZigType *ptr_type = get_pointer_to_type_extra2(ira->codegen,
2223622368 get_const_field_meta_type(ira, payload, "child", 4),
2223722369 get_const_field_bool(ira, payload, "is_const", 1),
2223822370 get_const_field_bool(ira, payload, "is_volatile", 2),
......@@ -22240,7 +22372,10 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInstruction *instruction, Zi
2224022372 bigint_as_u32(get_const_field_lit_int(ira, payload, "alignment", 3)),
2224122373 0, // bit_offset_in_host
2224222374 0, // host_int_bytes
22243 get_const_field_bool(ira, payload, "is_allowzero", 5)
22375 get_const_field_bool(ira, payload, "is_allowzero", 5),
22376 VECTOR_INDEX_NONE,
22377 nullptr,
22378 get_const_field_variant(ira, payload, "sentinel", 6)
2224422379 );
2224522380 if (size_enum_index != 2)
2224622381 return ptr_type;
......@@ -22252,7 +22387,7 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInstruction *instruction, Zi
2225222387 return get_array_type(ira->codegen,
2225322388 get_const_field_meta_type(ira, payload, "child", 1),
2225422389 bigint_as_u64(get_const_field_lit_int(ira, payload, "len", 0)),
22255 get_const_field_bool(ira, payload, "is_null_terminated", 2)
22390 get_const_field_variant(ira, payload, "sentinel", 2)
2225622391 );
2225722392 case ZigTypeIdComptimeFloat:
2225822393 return ira->codegen->builtin_types.entry_num_lit_float;
......@@ -22635,7 +22770,7 @@ static IrInstruction *ir_analyze_instruction_embed_file(IrAnalyze *ira, IrInstru
2263522770 }
2263622771
2263722772 ZigType *result_type = get_array_type(ira->codegen,
22638 ira->codegen->builtin_types.entry_u8, buf_len(file_contents), false);
22773 ira->codegen->builtin_types.entry_u8, buf_len(file_contents), nullptr);
2263922774 IrInstruction *result = ir_const(ira, &instruction->base, result_type);
2264022775 init_const_str_lit(ira->codegen, &result->value, file_contents);
2264122776 return result;
......@@ -25858,6 +25993,12 @@ static IrInstruction *ir_analyze_instruction_ptr_type(IrAnalyze *ira, IrInstruct
2585825993 result->value.data.x_lazy = &lazy_ptr_type->base;
2585925994 lazy_ptr_type->base.id = LazyValueIdPtrType;
2586025995
25996 if (instruction->sentinel != nullptr) {
25997 lazy_ptr_type->sentinel = instruction->sentinel->child;
25998 if (ir_resolve_const(ira, lazy_ptr_type->sentinel, LazyOk) == nullptr)
25999 return ira->codegen->invalid_instruction;
26000 }
26001
2586126002 lazy_ptr_type->elem_type = instruction->child_type->child;
2586226003 if (ir_resolve_type_lazy(ira, lazy_ptr_type->elem_type) == nullptr)
2586326004 return ira->codegen->invalid_instruction;
......@@ -27804,6 +27945,20 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ConstExprValue *val) {
2780427945 if (type_is_invalid(elem_type))
2780527946 return ErrorSemanticAnalyzeFail;
2780627947
27948 ConstExprValue *sentinel_val;
27949 if (lazy_slice_type->sentinel != nullptr) {
27950 if (type_is_invalid(lazy_slice_type->sentinel->value.type))
27951 return ErrorSemanticAnalyzeFail;
27952 IrInstruction *sentinel = ir_implicit_cast(ira, lazy_slice_type->sentinel, elem_type);
27953 if (type_is_invalid(sentinel->value.type))
27954 return ErrorSemanticAnalyzeFail;
27955 sentinel_val = ir_resolve_const(ira, sentinel, UndefBad);
27956 if (sentinel_val == nullptr)
27957 return ErrorSemanticAnalyzeFail;
27958 } else {
27959 sentinel_val = nullptr;
27960 }
27961
2780727962 uint32_t align_bytes = 0;
2780827963 if (lazy_slice_type->align_inst != nullptr) {
2780927964 if (!ir_resolve_align(ira, lazy_slice_type->align_inst, elem_type, &align_bytes))
......@@ -27849,11 +28004,12 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ConstExprValue *val) {
2784928004 ResolveStatusZeroBitsKnown : ResolveStatusAlignmentKnown;
2785028005 if ((err = type_resolve(ira->codegen, elem_type, needed_status)))
2785128006 return err;
27852 ZigType *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, elem_type,
28007 ZigType *slice_ptr_type = get_pointer_to_type_extra2(ira->codegen, elem_type,
2785328008 lazy_slice_type->is_const, lazy_slice_type->is_volatile,
27854 lazy_slice_type->is_null_terminated ? PtrLenNull : PtrLenUnknown,
28009 PtrLenUnknown,
2785528010 align_bytes,
27856 0, 0, lazy_slice_type->is_allowzero);
28011 0, 0, lazy_slice_type->is_allowzero,
28012 VECTOR_INDEX_NONE, nullptr, sentinel_val);
2785728013 val->special = ConstValSpecialStatic;
2785828014 assert(val->type->id == ZigTypeIdMetaType);
2785928015 val->data.x_type = get_slice_type(ira->codegen, slice_ptr_type);
......@@ -27867,6 +28023,20 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ConstExprValue *val) {
2786728023 if (type_is_invalid(elem_type))
2786828024 return ErrorSemanticAnalyzeFail;
2786928025
28026 ConstExprValue *sentinel_val;
28027 if (lazy_ptr_type->sentinel != nullptr) {
28028 if (type_is_invalid(lazy_ptr_type->sentinel->value.type))
28029 return ErrorSemanticAnalyzeFail;
28030 IrInstruction *sentinel = ir_implicit_cast(ira, lazy_ptr_type->sentinel, elem_type);
28031 if (type_is_invalid(sentinel->value.type))
28032 return ErrorSemanticAnalyzeFail;
28033 sentinel_val = ir_resolve_const(ira, sentinel, UndefBad);
28034 if (sentinel_val == nullptr)
28035 return ErrorSemanticAnalyzeFail;
28036 } else {
28037 sentinel_val = nullptr;
28038 }
28039
2787028040 uint32_t align_bytes = 0;
2787128041 if (lazy_ptr_type->align_inst != nullptr) {
2787228042 if (!ir_resolve_align(ira, lazy_ptr_type->align_inst, elem_type, &align_bytes))
......@@ -27909,10 +28079,10 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ConstExprValue *val) {
2790928079 }
2791028080 bool allow_zero = lazy_ptr_type->is_allowzero || lazy_ptr_type->ptr_len == PtrLenC;
2791128081 assert(val->type->id == ZigTypeIdMetaType);
27912 val->data.x_type = get_pointer_to_type_extra(ira->codegen, elem_type,
28082 val->data.x_type = get_pointer_to_type_extra2(ira->codegen, elem_type,
2791328083 lazy_ptr_type->is_const, lazy_ptr_type->is_volatile, lazy_ptr_type->ptr_len, align_bytes,
2791428084 lazy_ptr_type->bit_offset_in_host, lazy_ptr_type->host_int_bytes,
27915 allow_zero);
28085 allow_zero, VECTOR_INDEX_NONE, nullptr, sentinel_val);
2791628086 val->special = ConstValSpecialStatic;
2791728087 return ErrorNone;
2791828088 }
src/parser.cpp+111-73
......@@ -1833,8 +1833,10 @@ static AstNode *ast_parse_labeled_type_expr(ParseContext *pc) {
18331833 return loop;
18341834 }
18351835
1836 if (label != nullptr)
1837 ast_invalid_token_error(pc, peek_token(pc));
1836 if (label != nullptr) {
1837 put_back_token(pc);
1838 put_back_token(pc);
1839 }
18381840 return nullptr;
18391841}
18401842
......@@ -1931,15 +1933,11 @@ static AstNode *ast_parse_asm_output(ParseContext *pc) {
19311933
19321934// AsmOutputItem <- LBRACKET IDENTIFIER RBRACKET STRINGLITERAL LPAREN (MINUSRARROW TypeExpr / IDENTIFIER) RPAREN
19331935static AsmOutput *ast_parse_asm_output_item(ParseContext *pc) {
1934 Token *sym_name = eat_token_if(pc, TokenIdBracketUnderscoreBracket);
1935 if (sym_name == nullptr) {
1936 if (eat_token_if(pc, TokenIdLBracket) == nullptr) {
1937 return nullptr;
1938 } else {
1939 sym_name = expect_token(pc, TokenIdSymbol);
1940 expect_token(pc, TokenIdRBracket);
1941 }
1942 }
1936 if (eat_token_if(pc, TokenIdLBracket) == nullptr)
1937 return nullptr;
1938
1939 Token *sym_name = expect_token(pc, TokenIdSymbol);
1940 expect_token(pc, TokenIdRBracket);
19431941
19441942 Token *str = expect_token(pc, TokenIdStringLiteral);
19451943 expect_token(pc, TokenIdLParen);
......@@ -1954,7 +1952,7 @@ static AsmOutput *ast_parse_asm_output_item(ParseContext *pc) {
19541952 expect_token(pc, TokenIdRParen);
19551953
19561954 AsmOutput *res = allocate<AsmOutput>(1);
1957 res->asm_symbolic_name = (sym_name->id == TokenIdBracketUnderscoreBracket) ? buf_create_from_str("_") : token_buf(sym_name);
1955 res->asm_symbolic_name = token_buf(sym_name);
19581956 res->constraint = token_buf(str);
19591957 res->variable_name = token_buf(var_name);
19601958 res->return_type = return_type;
......@@ -1977,15 +1975,11 @@ static AstNode *ast_parse_asm_input(ParseContext *pc) {
19771975
19781976// AsmInputItem <- LBRACKET IDENTIFIER RBRACKET STRINGLITERAL LPAREN Expr RPAREN
19791977static AsmInput *ast_parse_asm_input_item(ParseContext *pc) {
1980 Token *sym_name = eat_token_if(pc, TokenIdBracketUnderscoreBracket);
1981 if (sym_name == nullptr) {
1982 if (eat_token_if(pc, TokenIdLBracket) == nullptr) {
1983 return nullptr;
1984 } else {
1985 sym_name = expect_token(pc, TokenIdSymbol);
1986 expect_token(pc, TokenIdRBracket);
1987 }
1988 }
1978 if (eat_token_if(pc, TokenIdLBracket) == nullptr)
1979 return nullptr;
1980
1981 Token *sym_name = expect_token(pc, TokenIdSymbol);
1982 expect_token(pc, TokenIdRBracket);
19891983
19901984 Token *constraint = expect_token(pc, TokenIdStringLiteral);
19911985 expect_token(pc, TokenIdLParen);
......@@ -1993,7 +1987,7 @@ static AsmInput *ast_parse_asm_input_item(ParseContext *pc) {
19931987 expect_token(pc, TokenIdRParen);
19941988
19951989 AsmInput *res = allocate<AsmInput>(1);
1996 res->asm_symbolic_name = (sym_name->id == TokenIdBracketUnderscoreBracket) ? buf_create_from_str("_") : token_buf(sym_name);
1990 res->asm_symbolic_name = token_buf(sym_name);
19971991 res->constraint = token_buf(constraint);
19981992 res->expr = expr;
19991993 return res;
......@@ -2613,42 +2607,28 @@ static AstNode *ast_parse_prefix_type_op(ParseContext *pc) {
26132607 put_back_token(pc);
26142608 }
26152609
2616 AstNode *array = ast_parse_array_type_start(pc);
2617 if (array != nullptr) {
2618 assert(array->type == NodeTypeArrayType);
2619 while (true) {
2620 if (eat_token_if(pc, TokenIdKeywordNull) != nullptr) {
2621 array->data.array_type.is_null_terminated = true;
2622 continue;
2623 }
2624
2625 Token *allowzero_token = eat_token_if(pc, TokenIdKeywordAllowZero);
2626 if (allowzero_token != nullptr) {
2627 array->data.array_type.allow_zero_token = allowzero_token;
2628 continue;
2629 }
2630
2631 AstNode *align_expr = ast_parse_byte_align(pc);
2632 if (align_expr != nullptr) {
2633 array->data.array_type.align_expr = align_expr;
2634 continue;
2635 }
2636
2637 if (eat_token_if(pc, TokenIdKeywordConst) != nullptr) {
2638 array->data.array_type.is_const = true;
2639 continue;
2640 }
2641
2642 if (eat_token_if(pc, TokenIdKeywordVolatile) != nullptr) {
2643 array->data.array_type.is_volatile = true;
2644 continue;
2610 Token *arr_init_lbracket = eat_token_if(pc, TokenIdLBracket);
2611 if (arr_init_lbracket != nullptr) {
2612 Token *underscore = eat_token_if(pc, TokenIdSymbol);
2613 if (underscore == nullptr) {
2614 put_back_token(pc);
2615 } else if (!buf_eql_str(token_buf(underscore), "_")) {
2616 put_back_token(pc);
2617 put_back_token(pc);
2618 } else {
2619 AstNode *sentinel = nullptr;
2620 Token *colon = eat_token_if(pc, TokenIdColon);
2621 if (colon != nullptr) {
2622 sentinel = ast_expect(pc, ast_parse_expr);
26452623 }
2646 break;
2624 expect_token(pc, TokenIdRBracket);
2625 AstNode *node = ast_create_node(pc, NodeTypeInferredArrayType, arr_init_lbracket);
2626 node->data.inferred_array_type.sentinel = sentinel;
2627 return node;
26472628 }
2648
2649 return array;
26502629 }
26512630
2631
26522632 AstNode *ptr = ast_parse_ptr_type_start(pc);
26532633 if (ptr != nullptr) {
26542634 assert(ptr->type == NodeTypePointerType);
......@@ -2657,11 +2637,6 @@ static AstNode *ast_parse_prefix_type_op(ParseContext *pc) {
26572637 if (child == nullptr)
26582638 child = ptr;
26592639 while (true) {
2660 if (eat_token_if(pc, TokenIdKeywordNull) != nullptr) {
2661 child->data.pointer_type.is_null_terminated = true;
2662 continue;
2663 }
2664
26652640 Token *allowzero_token = eat_token_if(pc, TokenIdKeywordAllowZero);
26662641 if (allowzero_token != nullptr) {
26672642 child->data.pointer_type.allow_zero_token = allowzero_token;
......@@ -2699,9 +2674,35 @@ static AstNode *ast_parse_prefix_type_op(ParseContext *pc) {
26992674 return ptr;
27002675 }
27012676
2702 Token *arr_init = eat_token_if(pc, TokenIdBracketUnderscoreBracket);
2703 if (arr_init != nullptr) {
2704 return ast_create_node(pc, NodeTypeInferredArrayType, arr_init);
2677 AstNode *array = ast_parse_array_type_start(pc);
2678 if (array != nullptr) {
2679 assert(array->type == NodeTypeArrayType);
2680 while (true) {
2681 Token *allowzero_token = eat_token_if(pc, TokenIdKeywordAllowZero);
2682 if (allowzero_token != nullptr) {
2683 array->data.array_type.allow_zero_token = allowzero_token;
2684 continue;
2685 }
2686
2687 AstNode *align_expr = ast_parse_byte_align(pc);
2688 if (align_expr != nullptr) {
2689 array->data.array_type.align_expr = align_expr;
2690 continue;
2691 }
2692
2693 if (eat_token_if(pc, TokenIdKeywordConst) != nullptr) {
2694 array->data.array_type.is_const = true;
2695 continue;
2696 }
2697
2698 if (eat_token_if(pc, TokenIdKeywordVolatile) != nullptr) {
2699 array->data.array_type.is_volatile = true;
2700 continue;
2701 }
2702 break;
2703 }
2704
2705 return array;
27052706 }
27062707
27072708
......@@ -2775,9 +2776,15 @@ static AstNode *ast_parse_array_type_start(ParseContext *pc) {
27752776 return nullptr;
27762777
27772778 AstNode *size = ast_parse_expr(pc);
2779 AstNode *sentinel = nullptr;
2780 Token *colon = eat_token_if(pc, TokenIdColon);
2781 if (colon != nullptr) {
2782 sentinel = ast_expect(pc, ast_parse_expr);
2783 }
27782784 expect_token(pc, TokenIdRBracket);
27792785 AstNode *res = ast_create_node(pc, NodeTypeArrayType, lbracket);
27802786 res->data.array_type.size = size;
2787 res->data.array_type.sentinel = sentinel;
27812788 return res;
27822789}
27832790
......@@ -2787,35 +2794,63 @@ static AstNode *ast_parse_array_type_start(ParseContext *pc) {
27872794// / PTRUNKNOWN
27882795// / PTRC
27892796static AstNode *ast_parse_ptr_type_start(ParseContext *pc) {
2797 AstNode *sentinel = nullptr;
2798
27902799 Token *asterisk = eat_token_if(pc, TokenIdStar);
27912800 if (asterisk != nullptr) {
2801 Token *colon = eat_token_if(pc, TokenIdColon);
2802 if (colon != nullptr) {
2803 sentinel = ast_expect(pc, ast_parse_expr);
2804 }
27922805 AstNode *res = ast_create_node(pc, NodeTypePointerType, asterisk);
27932806 res->data.pointer_type.star_token = asterisk;
2807 res->data.pointer_type.sentinel = sentinel;
27942808 return res;
27952809 }
27962810
27972811 Token *asterisk2 = eat_token_if(pc, TokenIdStarStar);
27982812 if (asterisk2 != nullptr) {
2813 Token *colon = eat_token_if(pc, TokenIdColon);
2814 if (colon != nullptr) {
2815 sentinel = ast_expect(pc, ast_parse_expr);
2816 }
27992817 AstNode *res = ast_create_node(pc, NodeTypePointerType, asterisk2);
28002818 AstNode *res2 = ast_create_node(pc, NodeTypePointerType, asterisk2);
28012819 res->data.pointer_type.star_token = asterisk2;
28022820 res2->data.pointer_type.star_token = asterisk2;
2821 res2->data.pointer_type.sentinel = sentinel;
28032822 res->data.pointer_type.op_expr = res2;
28042823 return res;
28052824 }
28062825
2807 Token *multptr = eat_token_if(pc, TokenIdBracketStarBracket);
2808 if (multptr != nullptr) {
2809 AstNode *res = ast_create_node(pc, NodeTypePointerType, multptr);
2810 res->data.pointer_type.star_token = multptr;
2811 return res;
2812 }
2826 Token *lbracket = eat_token_if(pc, TokenIdLBracket);
2827 if (lbracket != nullptr) {
2828 Token *star = eat_token_if(pc, TokenIdStar);
2829 if (star == nullptr) {
2830 put_back_token(pc);
2831 } else {
2832 Token *c_tok = eat_token_if(pc, TokenIdSymbol);
2833 if (c_tok != nullptr) {
2834 if (!buf_eql_str(token_buf(c_tok), "c")) {
2835 put_back_token(pc); // c symbol
2836 } else {
2837 expect_token(pc, TokenIdRBracket);
2838 AstNode *res = ast_create_node(pc, NodeTypePointerType, lbracket);
2839 res->data.pointer_type.star_token = c_tok;
2840 return res;
2841 }
2842 }
28132843
2814 Token *cptr = eat_token_if(pc, TokenIdBracketStarCBracket);
2815 if (cptr != nullptr) {
2816 AstNode *res = ast_create_node(pc, NodeTypePointerType, cptr);
2817 res->data.pointer_type.star_token = cptr;
2818 return res;
2844 Token *colon = eat_token_if(pc, TokenIdColon);
2845 if (colon != nullptr) {
2846 sentinel = ast_expect(pc, ast_parse_expr);
2847 }
2848 expect_token(pc, TokenIdRBracket);
2849 AstNode *res = ast_create_node(pc, NodeTypePointerType, lbracket);
2850 res->data.pointer_type.star_token = lbracket;
2851 res->data.pointer_type.sentinel = sentinel;
2852 return res;
2853 }
28192854 }
28202855
28212856 return nullptr;
......@@ -3093,10 +3128,12 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont
30933128 break;
30943129 case NodeTypeArrayType:
30953130 visit_field(&node->data.array_type.size, visit, context);
3131 visit_field(&node->data.array_type.sentinel, visit, context);
30963132 visit_field(&node->data.array_type.child_type, visit, context);
30973133 visit_field(&node->data.array_type.align_expr, visit, context);
30983134 break;
30993135 case NodeTypeInferredArrayType:
3136 visit_field(&node->data.array_type.sentinel, visit, context);
31003137 visit_field(&node->data.array_type.child_type, visit, context);
31013138 break;
31023139 case NodeTypeAnyFrameType:
......@@ -3106,6 +3143,7 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont
31063143 // none
31073144 break;
31083145 case NodeTypePointerType:
3146 visit_field(&node->data.pointer_type.sentinel, visit, context);
31093147 visit_field(&node->data.pointer_type.align_expr, visit, context);
31103148 visit_field(&node->data.pointer_type.op_expr, visit, context);
31113149 break;
src/tokenizer.cpp+1-68
......@@ -222,10 +222,6 @@ enum TokenizeState {
222222 TokenizeStateSawAtSign,
223223 TokenizeStateCharCode,
224224 TokenizeStateError,
225 TokenizeStateLBracket,
226 TokenizeStateLBracketStar,
227 TokenizeStateLBracketStarC,
228 TokenizeStateLBracketUnderscore,
229225};
230226
231227
......@@ -480,8 +476,8 @@ void tokenize(Buf *buf, Tokenization *out) {
480476 end_token(&t);
481477 break;
482478 case '[':
483 t.state = TokenizeStateLBracket;
484479 begin_token(&t, TokenIdLBracket);
480 end_token(&t);
485481 break;
486482 case ']':
487483 begin_token(&t, TokenIdRBracket);
......@@ -775,62 +771,6 @@ void tokenize(Buf *buf, Tokenization *out) {
775771 continue;
776772 }
777773 break;
778 case TokenizeStateLBracket:
779 switch (c) {
780 case '*':
781 t.state = TokenizeStateLBracketStar;
782 break;
783 case '_':
784 t.state = TokenizeStateLBracketUnderscore;
785 break;
786 default:
787 // reinterpret as just an lbracket
788 t.pos -= 1;
789 end_token(&t);
790 t.state = TokenizeStateStart;
791 continue;
792 }
793 break;
794 case TokenizeStateLBracketUnderscore:
795 switch (c) {
796 case ']':
797 set_token_id(&t, t.cur_tok, TokenIdBracketUnderscoreBracket);
798 end_token(&t);
799 t.state = TokenizeStateStart;
800 break;
801 default:
802 // reinterpret as just an lbracket
803 t.pos -= 2;
804 end_token(&t);
805 t.state = TokenizeStateStart;
806 continue;
807 }
808 break;
809 case TokenizeStateLBracketStar:
810 switch (c) {
811 case 'c':
812 t.state = TokenizeStateLBracketStarC;
813 set_token_id(&t, t.cur_tok, TokenIdBracketStarCBracket);
814 break;
815 case ']':
816 set_token_id(&t, t.cur_tok, TokenIdBracketStarBracket);
817 end_token(&t);
818 t.state = TokenizeStateStart;
819 break;
820 default:
821 invalid_char_error(&t, c);
822 }
823 break;
824 case TokenizeStateLBracketStarC:
825 switch (c) {
826 case ']':
827 end_token(&t);
828 t.state = TokenizeStateStart;
829 break;
830 default:
831 invalid_char_error(&t, c);
832 }
833 break;
834774 case TokenizeStateSawPlusPercent:
835775 switch (c) {
836776 case '=':
......@@ -1525,7 +1465,6 @@ void tokenize(Buf *buf, Tokenization *out) {
15251465 case TokenizeStateLineString:
15261466 case TokenizeStateLineStringEnd:
15271467 case TokenizeStateSawBarBar:
1528 case TokenizeStateLBracket:
15291468 case TokenizeStateDocComment:
15301469 case TokenizeStateContainerDocComment:
15311470 end_token(&t);
......@@ -1534,9 +1473,6 @@ void tokenize(Buf *buf, Tokenization *out) {
15341473 case TokenizeStateSawBackslash:
15351474 case TokenizeStateLineStringContinue:
15361475 case TokenizeStateLineStringContinueC:
1537 case TokenizeStateLBracketStar:
1538 case TokenizeStateLBracketStarC:
1539 case TokenizeStateLBracketUnderscore:
15401476 tokenize_error(&t, "unexpected EOF");
15411477 break;
15421478 case TokenizeStateLineComment:
......@@ -1576,8 +1512,6 @@ const char * token_name(TokenId id) {
15761512 case TokenIdBitShiftRight: return ">>";
15771513 case TokenIdBitShiftRightEq: return ">>=";
15781514 case TokenIdBitXorEq: return "^=";
1579 case TokenIdBracketStarBracket: return "[*]";
1580 case TokenIdBracketStarCBracket: return "[*c]";
15811515 case TokenIdCharLiteral: return "CharLiteral";
15821516 case TokenIdCmpEq: return "==";
15831517 case TokenIdCmpGreaterOrEq: return ">=";
......@@ -1681,7 +1615,6 @@ const char * token_name(TokenId id) {
16811615 case TokenIdTimesPercent: return "*%";
16821616 case TokenIdTimesPercentEq: return "*%=";
16831617 case TokenIdBarBarEq: return "||=";
1684 case TokenIdBracketUnderscoreBracket: return "[_]";
16851618 case TokenIdCount:
16861619 zig_unreachable();
16871620 }
src/tokenizer.hpp-3
......@@ -28,9 +28,6 @@ enum TokenId {
2828 TokenIdBitShiftRight,
2929 TokenIdBitShiftRightEq,
3030 TokenIdBitXorEq,
31 TokenIdBracketStarBracket,
32 TokenIdBracketStarCBracket,
33 TokenIdBracketUnderscoreBracket,
3431 TokenIdCharLiteral,
3532 TokenIdCmpEq,
3633 TokenIdCmpGreaterOrEq,
src/translate_c.cpp+2-4
......@@ -291,10 +291,9 @@ static TokenId ptr_len_to_token_id(PtrLen ptr_len) {
291291 case PtrLenSingle:
292292 return TokenIdStar;
293293 case PtrLenUnknown:
294 case PtrLenNull:
295 return TokenIdBracketStarBracket;
294 return TokenIdLBracket;
296295 case PtrLenC:
297 return TokenIdBracketStarCBracket;
296 return TokenIdSymbol;
298297 }
299298 zig_unreachable();
300299}
......@@ -303,7 +302,6 @@ static AstNode *trans_create_node_ptr_type(Context *c, bool is_const, bool is_vo
303302 AstNode *node = trans_create_node(c, NodeTypePointerType);
304303 node->data.pointer_type.star_token = allocate<ZigToken>(1);
305304 node->data.pointer_type.star_token->id = ptr_len_to_token_id(ptr_len);
306 node->data.pointer_type.is_null_terminated = (ptr_len == PtrLenNull);
307305 node->data.pointer_type.is_const = is_const;
308306 node->data.pointer_type.is_volatile = is_volatile;
309307 node->data.pointer_type.op_expr = child_node;
test/stage1/behavior/array.zig+1-1
......@@ -351,7 +351,7 @@ test "anonymous literal in array" {
351351test "access the null element of a null terminated array" {
352352 const S = struct {
353353 fn doTheTest() void {
354 var array: [4]null u8 = .{'a', 'o', 'e', 'u'};
354 var array: [4:0]u8 = .{'a', 'o', 'e', 'u'};
355355 comptime expect(array[4] == 0);
356356 var len: usize = 4;
357357 expect(array[len] == 0);
test/stage1/behavior/cast.zig+1-1
......@@ -226,7 +226,7 @@ fn testCastConstArrayRefToConstSlice() void {
226226 {
227227 const blah = "aoeu".*;
228228 const const_array_ref = &blah;
229 expect(@typeOf(const_array_ref) == *const [4]null u8);
229 expect(@typeOf(const_array_ref) == *const [4:0]u8);
230230 const slice: []const u8 = const_array_ref;
231231 expect(mem.eql(u8, slice, "aoeu"));
232232 }
test/stage1/behavior/misc.zig+2-2
......@@ -362,8 +362,8 @@ test "string concatenation" {
362362 const a = "OK" ++ " IT " ++ "WORKED";
363363 const b = "OK IT WORKED";
364364
365 comptime expect(@typeOf(a) == *const [12]null u8);
366 comptime expect(@typeOf(b) == *const [12]null u8);
365 comptime expect(@typeOf(a) == *const [12:0]u8);
366 comptime expect(@typeOf(b) == *const [12:0]u8);
367367
368368 const len = mem.len(u8, b);
369369 const len_with_null = len + 1;
test/stage1/behavior/pointers.zig+1-1
......@@ -205,7 +205,7 @@ test "null terminated pointer" {
205205 const S = struct {
206206 fn doTheTest() void {
207207 var array_with_zero = [_]u8{'h', 'e', 'l', 'l', 'o', 0};
208 var zero_ptr: [*]null const u8 = @ptrCast([*]null const u8, &array_with_zero);
208 var zero_ptr: [*:0]const u8 = @ptrCast([*:0]const u8, &array_with_zero);
209209 var no_zero_ptr: [*]const u8 = zero_ptr;
210210 expect(std.mem.eql(u8, std.mem.toSliceConst(u8, no_zero_ptr), "hello"));
211211 }
test/stage1/behavior/type.zig+4-4
......@@ -98,21 +98,21 @@ test "Type.Array" {
9898 .Array = TypeInfo.Array{
9999 .len = 123,
100100 .child = u8,
101 .is_null_terminated = false,
101 .sentinel = null,
102102 },
103103 }));
104104 testing.expect([2]u32 == @Type(TypeInfo{
105105 .Array = TypeInfo.Array{
106106 .len = 2,
107107 .child = u32,
108 .is_null_terminated = false,
108 .sentinel = null,
109109 },
110110 }));
111 testing.expect([2]null u32 == @Type(TypeInfo{
111 testing.expect([2:0]u32 == @Type(TypeInfo{
112112 .Array = TypeInfo.Array{
113113 .len = 2,
114114 .child = u32,
115 .is_null_terminated = true,
115 .sentinel = &0,
116116 },
117117 }));
118118 testTypes([_]type{ [1]u8, [30]usize, [7]bool });
test/stage1/behavior/type_info.zig+5-5
......@@ -71,17 +71,17 @@ test "type info: null terminated pointer type info" {
7171}
7272
7373fn testNullTerminatedPtr() void {
74 const ptr_info = @typeInfo([*]null u8);
74 const ptr_info = @typeInfo([*:0]u8);
7575 expect(@as(TypeId, ptr_info) == TypeId.Pointer);
7676 expect(ptr_info.Pointer.size == TypeInfo.Pointer.Size.Many);
7777 expect(ptr_info.Pointer.is_const == false);
7878 expect(ptr_info.Pointer.is_volatile == false);
7979 expect(ptr_info.Pointer.is_null_terminated == true);
8080
81 expect(@typeInfo([]null u8).Pointer.is_null_terminated == true);
82 expect(@typeInfo([10]null u8).Array.is_null_terminated == true);
83 expect(@typeInfo([10]null u8).Array.len == 10);
84 expect(@sizeOf([10]null u8) == 11);
81 expect(@typeInfo([:0]u8).Pointer.sentinel != null);
82 expect(@typeInfo([10:0]u8).Array.sentinel != null);
83 expect(@typeInfo([10:0]u8).Array.len == 10);
84 expect(@sizeOf([10:0]u8) == 11);
8585}
8686
8787test "type info: C pointer type info" {