authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-17 23:06:28-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-21 20:43:41-05:00
log21f344b3b903b41fd4793faa82a4ac26ad2544aa
tree713437712564e80b512629fa657da5f7cb56730b
parent1aa978f32e88b4c83fde95f62938c97c7c22164c
signaturelock-open Commit is signed but in an unrecognized format.

add null terminated pointers and arrays to self-hosted

as well as `@typeInfo` and `@Type`

14 files changed, 240 insertions(+), 123 deletions(-)

lib/std/builtin.zig+1
......@@ -161,6 +161,7 @@ pub const TypeInfo = union(enum) {
161161 pub const Array = struct {
162162 len: comptime_int,
163163 child: type,
164 is_null_terminated: bool,
164165 };
165166
166167 /// This data structure is used by the Zig language code generation and
lib/std/zig/ast.zig+13-3
......@@ -137,6 +137,7 @@ pub const Error = union(enum) {
137137 ExpectedCallOrFnProto: ExpectedCallOrFnProto,
138138 ExpectedSliceOrRBracket: ExpectedSliceOrRBracket,
139139 ExtraAlignQualifier: ExtraAlignQualifier,
140 ExtraNullQualifier: ExtraNullQualifier,
140141 ExtraConstQualifier: ExtraConstQualifier,
141142 ExtraVolatileQualifier: ExtraVolatileQualifier,
142143 ExtraAllowZeroQualifier: ExtraAllowZeroQualifier,
......@@ -184,6 +185,7 @@ pub const Error = union(enum) {
184185 .ExpectedCallOrFnProto => |*x| return x.render(tokens, stream),
185186 .ExpectedSliceOrRBracket => |*x| return x.render(tokens, stream),
186187 .ExtraAlignQualifier => |*x| return x.render(tokens, stream),
188 .ExtraNullQualifier => |*x| return x.render(tokens, stream),
187189 .ExtraConstQualifier => |*x| return x.render(tokens, stream),
188190 .ExtraVolatileQualifier => |*x| return x.render(tokens, stream),
189191 .ExtraAllowZeroQualifier => |*x| return x.render(tokens, stream),
......@@ -233,6 +235,7 @@ pub const Error = union(enum) {
233235 .ExpectedCallOrFnProto => |x| return x.node.firstToken(),
234236 .ExpectedSliceOrRBracket => |x| return x.token,
235237 .ExtraAlignQualifier => |x| return x.token,
238 .ExtraNullQualifier => |x| return x.token,
236239 .ExtraConstQualifier => |x| return x.token,
237240 .ExtraVolatileQualifier => |x| return x.token,
238241 .ExtraAllowZeroQualifier => |x| return x.token,
......@@ -293,6 +296,7 @@ pub const Error = union(enum) {
293296 pub const ExpectedPubItem = SimpleError("Expected function or variable declaration after pub");
294297 pub const UnattachedDocComment = SimpleError("Unattached documentation comment");
295298 pub const ExtraAlignQualifier = SimpleError("Extra align qualifier");
299 pub const ExtraNullQualifier = SimpleError("Extra null qualifier");
296300 pub const ExtraConstQualifier = SimpleError("Extra const qualifier");
297301 pub const ExtraVolatileQualifier = SimpleError("Extra volatile qualifier");
298302 pub const ExtraAllowZeroQualifier = SimpleError("Extra allowzero qualifier");
......@@ -1538,7 +1542,7 @@ pub const Node = struct {
15381542
15391543 pub const Op = union(enum) {
15401544 AddressOf,
1541 ArrayType: *Node,
1545 ArrayType: ArrayInfo,
15421546 Await,
15431547 BitNot,
15441548 BoolNot,
......@@ -1552,11 +1556,17 @@ pub const Node = struct {
15521556 Try,
15531557 };
15541558
1559 pub const ArrayInfo = struct {
1560 len_expr: *Node,
1561 null_token: ?TokenIndex,
1562 };
1563
15551564 pub const PtrInfo = struct {
15561565 allowzero_token: ?TokenIndex,
15571566 align_info: ?Align,
15581567 const_token: ?TokenIndex,
15591568 volatile_token: ?TokenIndex,
1569 null_token: ?TokenIndex,
15601570
15611571 pub const Align = struct {
15621572 node: *Node,
......@@ -1588,8 +1598,8 @@ pub const Node = struct {
15881598 }
15891599 },
15901600
1591 Op.ArrayType => |size_expr| {
1592 if (i < 1) return size_expr;
1601 Op.ArrayType => |array_info| {
1602 if (i < 1) return array_info.len_expr;
15931603 i -= 1;
15941604 },
15951605
lib/std/zig/parse.zig+31-8
......@@ -1085,7 +1085,7 @@ fn parseInitList(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node.Suf
10851085 const node = try arena.create(Node.SuffixOp);
10861086 node.* = Node.SuffixOp{
10871087 .base = Node{ .id = .SuffixOp },
1088 .lhs = .{.node = undefined}, // set by caller
1088 .lhs = .{ .node = undefined }, // set by caller
10891089 .op = op,
10901090 .rtoken = try expectToken(it, tree, .RBrace),
10911091 };
......@@ -1138,7 +1138,7 @@ fn parseSuffixExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
11381138
11391139 while (try parseSuffixOp(arena, it, tree)) |node| {
11401140 switch (node.id) {
1141 .SuffixOp => node.cast(Node.SuffixOp).?.lhs = .{.node = res},
1141 .SuffixOp => node.cast(Node.SuffixOp).?.lhs = .{ .node = res },
11421142 .InfixOp => node.cast(Node.InfixOp).?.lhs = res,
11431143 else => unreachable,
11441144 }
......@@ -1154,7 +1154,7 @@ fn parseSuffixExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
11541154 const node = try arena.create(Node.SuffixOp);
11551155 node.* = Node.SuffixOp{
11561156 .base = Node{ .id = .SuffixOp },
1157 .lhs = .{.node = res},
1157 .lhs = .{ .node = res },
11581158 .op = Node.SuffixOp.Op{
11591159 .Call = Node.SuffixOp.Op.Call{
11601160 .params = params.list,
......@@ -1171,7 +1171,7 @@ fn parseSuffixExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
11711171 while (true) {
11721172 if (try parseSuffixOp(arena, it, tree)) |node| {
11731173 switch (node.id) {
1174 .SuffixOp => node.cast(Node.SuffixOp).?.lhs = .{.node = res},
1174 .SuffixOp => node.cast(Node.SuffixOp).?.lhs = .{ .node = res },
11751175 .InfixOp => node.cast(Node.InfixOp).?.lhs = res,
11761176 else => unreachable,
11771177 }
......@@ -1182,7 +1182,7 @@ fn parseSuffixExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node {
11821182 const call = try arena.create(Node.SuffixOp);
11831183 call.* = Node.SuffixOp{
11841184 .base = Node{ .id = .SuffixOp },
1185 .lhs = .{.node = res},
1185 .lhs = .{ .node = res },
11861186 .op = Node.SuffixOp.Op{
11871187 .Call = Node.SuffixOp.Op.Call{
11881188 .params = params.list,
......@@ -1531,7 +1531,7 @@ fn parseAnonLiteral(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node
15311531
15321532 // anon container literal
15331533 if (try parseInitList(arena, it, tree)) |node| {
1534 node.lhs = .{.dot = dot};
1534 node.lhs = .{ .dot = dot };
15351535 return &node.base;
15361536 }
15371537
......@@ -2252,6 +2252,16 @@ fn parsePrefixTypeOp(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node
22522252 .SliceType => |*slice_type| {
22532253 // Collect pointer qualifiers in any order, but disallow duplicates
22542254 while (true) {
2255 if (eatToken(it, .Keyword_null)) |null_token| {
2256 if (slice_type.null_token != null) {
2257 try tree.errors.push(AstError{
2258 .ExtraNullQualifier = AstError.ExtraNullQualifier{ .token = it.index },
2259 });
2260 return error.ParseError;
2261 }
2262 slice_type.null_token = null_token;
2263 continue;
2264 }
22552265 if (try parseByteAlign(arena, it, tree)) |align_expr| {
22562266 if (slice_type.align_info != null) {
22572267 try tree.errors.push(AstError{
......@@ -2313,6 +2323,10 @@ fn parsePrefixTypeOp(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node
23132323 &prefix_op.op.PtrType;
23142324
23152325 while (true) {
2326 if (eatToken(it, .Keyword_null)) |null_token| {
2327 ptr_info.null_token = null_token;
2328 continue;
2329 }
23162330 if (eatToken(it, .Keyword_align)) |align_token| {
23172331 const lparen = try expectToken(it, tree, .LParen);
23182332 const expr_node = try expectNode(arena, it, tree, parseExpr, AstError{
......@@ -2460,9 +2474,15 @@ fn parseArrayTypeStart(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*No
24602474 const lbracket = eatToken(it, .LBracket) orelse return null;
24612475 const expr = try parseExpr(arena, it, tree);
24622476 const rbracket = try expectToken(it, tree, .RBracket);
2477 const null_token = eatToken(it, .Keyword_null);
24632478
2464 const op = if (expr) |element_type|
2465 Node.PrefixOp.Op{ .ArrayType = element_type }
2479 const op = if (expr) |len_expr|
2480 Node.PrefixOp.Op{
2481 .ArrayType = .{
2482 .len_expr = len_expr,
2483 .null_token = null_token,
2484 },
2485 }
24662486 else
24672487 Node.PrefixOp.Op{
24682488 .SliceType = Node.PrefixOp.PtrInfo{
......@@ -2470,6 +2490,7 @@ fn parseArrayTypeStart(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*No
24702490 .align_info = null,
24712491 .const_token = null,
24722492 .volatile_token = null,
2493 .null_token = null,
24732494 },
24742495 };
24752496
......@@ -2505,6 +2526,7 @@ fn parsePtrTypeStart(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node
25052526 .align_info = null,
25062527 .const_token = null,
25072528 .volatile_token = null,
2529 .null_token = null,
25082530 },
25092531 },
25102532 .rhs = undefined, // set by caller
......@@ -2522,6 +2544,7 @@ fn parsePtrTypeStart(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node
25222544 .align_info = null,
25232545 .const_token = null,
25242546 .volatile_token = null,
2547 .null_token = null,
25252548 },
25262549 },
25272550 .rhs = undefined, // set by caller
lib/std/zig/parser_test.zig+3
......@@ -1552,6 +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;
15551556 \\
15561557 );
15571558}
......@@ -1562,6 +1563,7 @@ test "zig fmt: slice attributes" {
15621563 \\extern fn f2(s: **align(1) *const *volatile u8) c_int;
15631564 \\extern fn f3(s: *align(1) const *align(1) volatile *const volatile u8) c_int;
15641565 \\extern fn f4(s: *align(1) const volatile u8) c_int;
1566 \\extern fn f5(s: [*]null align(1) const volatile u8) c_int;
15651567 \\
15661568 );
15671569}
......@@ -1889,6 +1891,7 @@ test "zig fmt: arrays" {
18891891 \\ 2,
18901892 \\ };
18911893 \\ const a: [0]u8 = []u8{};
1894 \\ const x: [4]null u8 = undefined;
18921895 \\}
18931896 \\
18941897 );
lib/std/zig/render.zig+9-3
......@@ -423,6 +423,9 @@ fn renderExpression(
423423 else => @as(usize, 0),
424424 };
425425 try renderTokenOffset(tree, stream, prefix_op_node.op_token, indent, start_col, Space.None, star_offset); // *
426 if (ptr_info.null_token) |null_token| {
427 try renderToken(tree, stream, null_token, indent, start_col, Space.Space); // null
428 }
426429 if (ptr_info.allowzero_token) |allowzero_token| {
427430 try renderToken(tree, stream, allowzero_token, indent, start_col, Space.Space); // allowzero
428431 }
......@@ -499,9 +502,9 @@ fn renderExpression(
499502 }
500503 },
501504
502 ast.Node.PrefixOp.Op.ArrayType => |array_index| {
505 ast.Node.PrefixOp.Op.ArrayType => |array_info| {
503506 const lbracket = prefix_op_node.op_token;
504 const rbracket = tree.nextToken(array_index.lastToken());
507 const rbracket = tree.nextToken(array_info.len_expr.lastToken());
505508
506509 try renderToken(tree, stream, lbracket, indent, start_col, Space.None); // [
507510
......@@ -509,7 +512,7 @@ fn renderExpression(
509512 const ends_with_comment = tree.tokens.at(rbracket - 1).id == .LineComment;
510513 const new_indent = if (ends_with_comment) indent + indent_delta else indent;
511514 const new_space = if (ends_with_comment) Space.Newline else Space.None;
512 try renderExpression(allocator, stream, tree, new_indent, start_col, array_index, new_space);
515 try renderExpression(allocator, stream, tree, new_indent, start_col, array_info.len_expr, new_space);
513516 if (starts_with_comment) {
514517 try stream.writeByte('\n');
515518 }
......@@ -517,6 +520,9 @@ fn renderExpression(
517520 try stream.writeByteNTimes(' ', indent);
518521 }
519522 try renderToken(tree, stream, rbracket, indent, start_col, Space.None); // ]
523 if (array_info.null_token) |null_token| {
524 try renderToken(tree, stream, null_token, indent, start_col, Space.Space); // null
525 }
520526 },
521527 ast.Node.PrefixOp.Op.BitNot,
522528 ast.Node.PrefixOp.Op.BoolNot,
src-self-hosted/translate_c.zig+1
......@@ -1118,6 +1118,7 @@ fn transCreateNodePtrType(
11181118 .align_info = null,
11191119 .const_token = if (is_const) try appendToken(c, .Keyword_const, "const") else null,
11201120 .volatile_token = if (is_volatile) try appendToken(c, .Keyword_volatile, "volatile") else null,
1121 .null_token = null,
11211122 },
11221123 },
11231124 .rhs = undefined, // translate and set afterward
src/all_types.hpp+5
......@@ -358,6 +358,7 @@ struct LazyValueSliceType {
358358 bool is_const;
359359 bool is_volatile;
360360 bool is_allowzero;
361 bool is_null_terminated;
361362};
362363
363364struct LazyValuePtrType {
......@@ -1234,6 +1235,7 @@ struct ZigTypeFloat {
12341235struct ZigTypeArray {
12351236 ZigType *child_type;
12361237 uint64_t len;
1238 bool is_null_terminated;
12371239};
12381240
12391241struct TypeStructField {
......@@ -1775,6 +1777,7 @@ struct TypeId {
17751777 struct {
17761778 ZigType *child_type;
17771779 uint64_t size;
1780 bool is_null_terminated;
17781781 } array;
17791782 struct {
17801783 bool is_signed;
......@@ -2986,6 +2989,7 @@ struct IrInstructionArrayType {
29862989
29872990 IrInstruction *size;
29882991 IrInstruction *child_type;
2992 bool is_null_terminated;
29892993};
29902994
29912995struct IrInstructionPtrType {
......@@ -3015,6 +3019,7 @@ struct IrInstructionSliceType {
30153019 bool is_const;
30163020 bool is_volatile;
30173021 bool is_allow_zero;
3022 bool is_null_terminated;
30183023};
30193024
30203025struct IrInstructionGlobalAsm {
src/analyze.cpp+18-13
......@@ -752,11 +752,12 @@ ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payloa
752752 return entry;
753753}
754754
755ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size) {
755ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size, bool is_null_terminated) {
756756 TypeId type_id = {};
757757 type_id.id = ZigTypeIdArray;
758758 type_id.data.array.child_type = child_type;
759759 type_id.data.array.size = array_size;
760 type_id.data.array.is_null_terminated = is_null_terminated;
760761 auto existing_entry = g->type_table.maybe_get(type_id);
761762 if (existing_entry) {
762763 return existing_entry->value;
......@@ -769,12 +770,14 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size) {
769770 buf_resize(&entry->name, 0);
770771 buf_appendf(&entry->name, "[%" ZIG_PRI_u64 "]%s", array_size, buf_ptr(&child_type->name));
771772
772 entry->size_in_bits = child_type->size_in_bits * array_size;
773 size_t full_array_size = array_size + (is_null_terminated ? 1 : 0);
774 entry->size_in_bits = child_type->size_in_bits * full_array_size;
773775 entry->abi_align = child_type->abi_align;
774 entry->abi_size = child_type->abi_size * array_size;
776 entry->abi_size = child_type->abi_size * full_array_size;
775777
776778 entry->data.array.child_type = child_type;
777779 entry->data.array.len = array_size;
780 entry->data.array.is_null_terminated = is_null_terminated;
778781
779782 g->type_table.put(type_id, entry);
780783 return entry;
......@@ -782,7 +785,7 @@ ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size) {
782785
783786ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
784787 assert(ptr_type->id == ZigTypeIdPointer);
785 assert(ptr_type->data.pointer.ptr_len == PtrLenUnknown);
788 assert(ptr_type->data.pointer.ptr_len == PtrLenUnknown || ptr_type->data.pointer.ptr_len == PtrLenNull);
786789
787790 ZigType **parent_pointer = &ptr_type->data.pointer.slice_parent;
788791 if (*parent_pointer) {
......@@ -5615,7 +5618,7 @@ void init_const_str_lit(CodeGen *g, ConstExprValue *const_val, Buf *str) {
56155618 }
56165619
56175620 const_val->special = ConstValSpecialStatic;
5618 const_val->type = get_array_type(g, g->builtin_types.entry_u8, buf_len(str));
5621 const_val->type = get_array_type(g, g->builtin_types.entry_u8, buf_len(str), false);
56195622 const_val->data.x_array.special = ConstArraySpecialBuf;
56205623 const_val->data.x_array.data.s_buf = str;
56215624
......@@ -5633,7 +5636,7 @@ void init_const_c_str_lit(CodeGen *g, ConstExprValue *const_val, Buf *str) {
56335636 size_t len_with_null = buf_len(str) + 1;
56345637 ConstExprValue *array_val = create_const_vals(1);
56355638 array_val->special = ConstValSpecialStatic;
5636 array_val->type = get_array_type(g, g->builtin_types.entry_u8, len_with_null);
5639 array_val->type = get_array_type(g, g->builtin_types.entry_u8, len_with_null, false);
56375640 // TODO buf optimization
56385641 array_val->data.x_array.data.s_none.elements = create_const_vals(len_with_null);
56395642 for (size_t i = 0; i < buf_len(str); i += 1) {
......@@ -6071,7 +6074,7 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {
60716074
60726075 fields.append({"@stack_trace", get_stack_trace_type(g), 0});
60736076 fields.append({"@instruction_addresses",
6074 get_array_type(g, g->builtin_types.entry_usize, stack_trace_ptr_count), 0});
6077 get_array_type(g, g->builtin_types.entry_usize, stack_trace_ptr_count, false), 0});
60756078 }
60766079
60776080 frame_type->data.frame.locals_struct = get_struct_type(g, buf_ptr(&frame_type->name),
......@@ -6279,7 +6282,7 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {
62796282 if (codegen_fn_has_err_ret_tracing_stack(g, fn, true)) {
62806283 fields.append({"@stack_trace", get_stack_trace_type(g), 0});
62816284 fields.append({"@instruction_addresses",
6282 get_array_type(g, g->builtin_types.entry_usize, stack_trace_ptr_count), 0});
6285 get_array_type(g, g->builtin_types.entry_usize, stack_trace_ptr_count, false), 0});
62836286 }
62846287
62856288 for (size_t alloca_i = 0; alloca_i < fn->alloca_gen_list.length; alloca_i += 1) {
......@@ -7043,8 +7046,9 @@ uint32_t type_id_hash(TypeId x) {
70437046 (((uint32_t)x.data.pointer.vector_index) ^ (uint32_t)0x19199716) +
70447047 (((uint32_t)x.data.pointer.host_int_bytes) ^ (uint32_t)529908881);
70457048 case ZigTypeIdArray:
7046 return hash_ptr(x.data.array.child_type) +
7047 ((uint32_t)x.data.array.size ^ (uint32_t)2122979968);
7049 return hash_ptr(x.data.array.child_type) *
7050 ((uint32_t)x.data.array.size ^ (uint32_t)2122979968) *
7051 ((uint32_t)x.data.array.is_null_terminated ^ (uint32_t)2048352596);
70487052 case ZigTypeIdInt:
70497053 return (x.data.integer.is_signed ? (uint32_t)2652528194 : (uint32_t)163929201) +
70507054 (((uint32_t)x.data.integer.bit_count) ^ (uint32_t)2998081557);
......@@ -7106,7 +7110,8 @@ bool type_id_eql(TypeId a, TypeId b) {
71067110 );
71077111 case ZigTypeIdArray:
71087112 return a.data.array.child_type == b.data.array.child_type &&
7109 a.data.array.size == b.data.array.size;
7113 a.data.array.size == b.data.array.size &&
7114 a.data.array.is_null_terminated == b.data.array.is_null_terminated;
71107115 case ZigTypeIdInt:
71117116 return a.data.integer.is_signed == b.data.integer.is_signed &&
71127117 a.data.integer.bit_count == b.data.integer.bit_count;
......@@ -8292,7 +8297,7 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta
82928297 size_t padding_bytes = union_type->data.unionation.union_abi_size - most_aligned_union_member->type_entry->abi_size;
82938298 if (padding_bytes > 0) {
82948299 ZigType *u8_type = get_int_type(g, false, 8);
8295 ZigType *padding_array = get_array_type(g, u8_type, padding_bytes);
8300 ZigType *padding_array = get_array_type(g, u8_type, padding_bytes, false);
82968301 LLVMTypeRef union_element_types[] = {
82978302 most_aligned_union_member->type_entry->llvm_type,
82988303 get_llvm_type(g, padding_array),
......@@ -8326,7 +8331,7 @@ static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveSta
83268331 union_type_ref = get_llvm_type(g, most_aligned_union_member->type_entry);
83278332 } else {
83288333 ZigType *u8_type = get_int_type(g, false, 8);
8329 ZigType *padding_array = get_array_type(g, u8_type, padding_bytes);
8334 ZigType *padding_array = get_array_type(g, u8_type, padding_bytes, false);
83308335 LLVMTypeRef union_element_types[] = {
83318336 get_llvm_type(g, most_aligned_union_member->type_entry),
83328337 get_llvm_type(g, padding_array),
src/analyze.hpp+1-1
......@@ -33,7 +33,7 @@ ZigType **get_c_int_type_ptr(CodeGen *g, CIntType c_int_type);
3333ZigType *get_c_int_type(CodeGen *g, CIntType c_int_type);
3434ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id);
3535ZigType *get_optional_type(CodeGen *g, ZigType *child_type);
36ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size);
36ZigType *get_array_type(CodeGen *g, ZigType *child_type, uint64_t array_size, bool is_null_terminated);
3737ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type);
3838ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind,
3939 AstNode *decl_node, const char *full_name, Buf *bare_name, ContainerLayout layout);
src/codegen.cpp+2-2
......@@ -7465,7 +7465,7 @@ static void do_code_gen(CodeGen *g) {
74657465 !is_async && !have_err_ret_trace_arg;
74667466 LLVMValueRef err_ret_array_val = nullptr;
74677467 if (have_err_ret_trace_stack) {
7468 ZigType *array_type = get_array_type(g, g->builtin_types.entry_usize, stack_trace_ptr_count);
7468 ZigType *array_type = get_array_type(g, g->builtin_types.entry_usize, stack_trace_ptr_count, false);
74697469 err_ret_array_val = build_alloca(g, array_type, "error_return_trace_addresses", get_abi_alignment(g, array_type));
74707470
74717471 (void)get_llvm_type(g, get_stack_trace_type(g));
......@@ -9067,7 +9067,7 @@ static void create_test_compile_var_and_add_test_runner(CodeGen *g) {
90679067 zig_unreachable();
90689068
90699069 ConstExprValue *test_fn_array = create_const_vals(1);
9070 test_fn_array->type = get_array_type(g, struct_type, g->test_fns.length);
9070 test_fn_array->type = get_array_type(g, struct_type, g->test_fns.length, false);
90719071 test_fn_array->special = ConstValSpecialStatic;
90729072 test_fn_array->data.x_array.data.s_none.elements = create_const_vals(g->test_fns.length);
90739073
src/ir.cpp+43-24
......@@ -1772,11 +1772,12 @@ static IrInstruction *ir_build_set_float_mode(IrBuilder *irb, Scope *scope, AstN
17721772}
17731773
17741774static IrInstruction *ir_build_array_type(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *size,
1775 IrInstruction *child_type)
1775 IrInstruction *child_type, bool is_null_terminated)
17761776{
17771777 IrInstructionArrayType *instruction = ir_build_instruction<IrInstructionArrayType>(irb, scope, source_node);
17781778 instruction->size = size;
17791779 instruction->child_type = child_type;
1780 instruction->is_null_terminated = is_null_terminated;
17801781
17811782 ir_ref_instruction(size, irb->current_basic_block);
17821783 ir_ref_instruction(child_type, irb->current_basic_block);
......@@ -1795,7 +1796,8 @@ static IrInstruction *ir_build_anyframe_type(IrBuilder *irb, Scope *scope, AstNo
17951796 return &instruction->base;
17961797}
17971798static IrInstruction *ir_build_slice_type(IrBuilder *irb, Scope *scope, AstNode *source_node,
1798 IrInstruction *child_type, bool is_const, bool is_volatile, IrInstruction *align_value, bool is_allow_zero)
1799 IrInstruction *child_type, bool is_const, bool is_volatile, IrInstruction *align_value, bool is_allow_zero,
1800 bool is_null_terminated)
17991801{
18001802 IrInstructionSliceType *instruction = ir_build_instruction<IrInstructionSliceType>(irb, scope, source_node);
18011803 instruction->is_const = is_const;
......@@ -1803,6 +1805,7 @@ static IrInstruction *ir_build_slice_type(IrBuilder *irb, Scope *scope, AstNode
18031805 instruction->child_type = child_type;
18041806 instruction->align_value = align_value;
18051807 instruction->is_allow_zero = is_allow_zero;
1808 instruction->is_null_terminated = is_null_terminated;
18061809
18071810 ir_ref_instruction(child_type, irb->current_basic_block);
18081811 if (align_value) ir_ref_instruction(align_value, irb->current_basic_block);
......@@ -6216,7 +6219,7 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A
62166219 return elem_type;
62176220 size_t item_count = container_init_expr->entries.length;
62186221 IrInstruction *item_count_inst = ir_build_const_usize(irb, scope, node, item_count);
6219 container_type = ir_build_array_type(irb, scope, node, item_count_inst, elem_type);
6222 container_type = ir_build_array_type(irb, scope, node, item_count_inst, elem_type, false);
62206223 } else {
62216224 container_type = ir_gen_node(irb, container_init_expr->type, scope);
62226225 if (container_type == irb->codegen->invalid_instruction)
......@@ -6944,6 +6947,7 @@ static IrInstruction *ir_gen_array_type(IrBuilder *irb, Scope *scope, AstNode *n
69446947 bool is_const = node->data.array_type.is_const;
69456948 bool is_volatile = node->data.array_type.is_volatile;
69466949 bool is_allow_zero = node->data.array_type.allow_zero_token != nullptr;
6950 bool is_null_terminated = node->data.array_type.is_null_terminated;
69476951 AstNode *align_expr = node->data.array_type.align_expr;
69486952
69496953 Scope *comptime_scope = create_comptime_scope(irb->codegen, node, scope);
......@@ -6973,7 +6977,7 @@ static IrInstruction *ir_gen_array_type(IrBuilder *irb, Scope *scope, AstNode *n
69736977 if (child_type == irb->codegen->invalid_instruction)
69746978 return child_type;
69756979
6976 return ir_build_array_type(irb, scope, node, size_value, child_type);
6980 return ir_build_array_type(irb, scope, node, size_value, child_type, is_null_terminated);
69776981 } else {
69786982 IrInstruction *align_value;
69796983 if (align_expr != nullptr) {
......@@ -6988,7 +6992,8 @@ static IrInstruction *ir_gen_array_type(IrBuilder *irb, Scope *scope, AstNode *n
69886992 if (child_type == irb->codegen->invalid_instruction)
69896993 return child_type;
69906994
6991 return ir_build_slice_type(irb, scope, node, child_type, is_const, is_volatile, align_value, is_allow_zero);
6995 return ir_build_slice_type(irb, scope, node, child_type, is_const, is_volatile, align_value, is_allow_zero,
6996 is_null_terminated);
69926997 }
69936998}
69946999
......@@ -10631,6 +10636,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
1063110636 // *[N]T to []T
1063210637 // *[N]T to E![]T
1063310638 if (cur_type->id == ZigTypeIdPointer &&
10639 cur_type->data.pointer.ptr_len == PtrLenSingle &&
1063410640 cur_type->data.pointer.child_type->id == ZigTypeIdArray &&
1063510641 ((prev_type->id == ZigTypeIdErrorUnion && is_slice(prev_type->data.error_union.payload_type)) ||
1063610642 is_slice(prev_type)))
......@@ -10639,7 +10645,8 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
1063910645 ZigType *slice_type = (prev_type->id == ZigTypeIdErrorUnion) ?
1064010646 prev_type->data.error_union.payload_type : prev_type;
1064110647 ZigType *slice_ptr_type = slice_type->data.structure.fields[slice_ptr_index]->type_entry;
10642 if ((slice_ptr_type->data.pointer.is_const || array_type->data.array.len == 0) &&
10648 if ((slice_ptr_type->data.pointer.is_const || array_type->data.array.len == 0 ||
10649 !cur_type->data.pointer.is_const) &&
1064310650 types_match_const_cast_only(ira,
1064410651 slice_ptr_type->data.pointer.child_type,
1064510652 array_type->data.array.child_type, source_node, false).id == ConstCastResultIdOk)
......@@ -10653,6 +10660,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
1065310660 // *[N]T to E![]T
1065410661 if (prev_type->id == ZigTypeIdPointer &&
1065510662 prev_type->data.pointer.child_type->id == ZigTypeIdArray &&
10663 prev_type->data.pointer.ptr_len == PtrLenSingle &&
1065610664 ((cur_type->id == ZigTypeIdErrorUnion && is_slice(cur_type->data.error_union.payload_type)) ||
1065710665 is_slice(cur_type)))
1065810666 {
......@@ -10660,7 +10668,8 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
1066010668 ZigType *slice_type = (cur_type->id == ZigTypeIdErrorUnion) ?
1066110669 cur_type->data.error_union.payload_type : cur_type;
1066210670 ZigType *slice_ptr_type = slice_type->data.structure.fields[slice_ptr_index]->type_entry;
10663 if ((slice_ptr_type->data.pointer.is_const || array_type->data.array.len == 0) &&
10671 if ((slice_ptr_type->data.pointer.is_const || array_type->data.array.len == 0 ||
10672 !prev_type->data.pointer.is_const) &&
1066410673 types_match_const_cast_only(ira,
1066510674 slice_ptr_type->data.pointer.child_type,
1066610675 array_type->data.array.child_type, source_node, false).id == ConstCastResultIdOk)
......@@ -14893,7 +14902,7 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i
1489314902 ConstExprValue *out_array_val;
1489414903 size_t new_len = (op1_array_end - op1_array_index) + (op2_array_end - op2_array_index);
1489514904 if (op1_type->id == ZigTypeIdArray || op2_type->id == ZigTypeIdArray) {
14896 result->value.type = get_array_type(ira->codegen, child_type, new_len);
14905 result->value.type = get_array_type(ira->codegen, child_type, new_len, false);
1489714906
1489814907 out_array_val = out_val;
1489914908 } else if (is_slice(op1_type) || is_slice(op2_type)) {
......@@ -14902,7 +14911,7 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i
1490214911 result->value.type = get_slice_type(ira->codegen, ptr_type);
1490314912 out_array_val = create_const_vals(1);
1490414913 out_array_val->special = ConstValSpecialStatic;
14905 out_array_val->type = get_array_type(ira->codegen, child_type, new_len);
14914 out_array_val->type = get_array_type(ira->codegen, child_type, new_len, false);
1490614915
1490714916 out_val->data.x_struct.fields = alloc_const_vals_ptrs(2);
1490814917
......@@ -14923,7 +14932,7 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i
1492314932
1492414933 out_array_val = create_const_vals(1);
1492514934 out_array_val->special = ConstValSpecialStatic;
14926 out_array_val->type = get_array_type(ira->codegen, child_type, new_len);
14935 out_array_val->type = get_array_type(ira->codegen, child_type, new_len, false);
1492714936 out_val->data.x_ptr.special = ConstPtrSpecialBaseArray;
1492814937 out_val->data.x_ptr.data.base_array.is_cstr = true;
1492914938 out_val->data.x_ptr.data.base_array.array_val = out_array_val;
......@@ -14994,7 +15003,7 @@ static IrInstruction *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp *
1499415003 ZigType *child_type = array_type->data.array.child_type;
1499515004
1499615005 IrInstruction *result = ir_const(ira, &instruction->base,
14997 get_array_type(ira->codegen, child_type, new_array_len));
15006 get_array_type(ira->codegen, child_type, new_array_len, false));
1499815007 ConstExprValue *out_val = &result->value;
1499915008 if (array_val->data.x_array.special == ConstArraySpecialUndef) {
1500015009 out_val->data.x_array.special = ConstArraySpecialUndef;
......@@ -19311,6 +19320,7 @@ static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1931119320 lazy_slice_type->is_const = slice_type_instruction->is_const;
1931219321 lazy_slice_type->is_volatile = slice_type_instruction->is_volatile;
1931319322 lazy_slice_type->is_allowzero = slice_type_instruction->is_allow_zero;
19323 lazy_slice_type->is_null_terminated = slice_type_instruction->is_null_terminated;
1931419324
1931519325 return result;
1931619326}
......@@ -19420,7 +19430,8 @@ static IrInstruction *ir_analyze_instruction_array_type(IrAnalyze *ira,
1942019430 {
1942119431 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusSizeKnown)))
1942219432 return ira->codegen->invalid_instruction;
19423 ZigType *result_type = get_array_type(ira->codegen, child_type, size);
19433 ZigType *result_type = get_array_type(ira->codegen, child_type, size,
19434 array_type_instruction->is_null_terminated);
1942419435 return ir_const_type(ira, &array_type_instruction->base, result_type);
1942519436 }
1942619437 }
......@@ -20496,7 +20507,7 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
2049620507 if (container_type->id == ZigTypeIdArray) {
2049720508 ZigType *child_type = container_type->data.array.child_type;
2049820509 if (container_type->data.array.len != elem_count) {
20499 ZigType *literal_type = get_array_type(ira->codegen, child_type, elem_count);
20510 ZigType *literal_type = get_array_type(ira->codegen, child_type, elem_count, false);
2050020511
2050120512 ir_add_error(ira, &instruction->base,
2050220513 buf_sprintf("expected %s literal, found %s literal",
......@@ -20983,7 +20994,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr
2098320994
2098420995 ConstExprValue *declaration_array = create_const_vals(1);
2098520996 declaration_array->special = ConstValSpecialStatic;
20986 declaration_array->type = get_array_type(ira->codegen, type_info_declaration_type, declaration_count);
20997 declaration_array->type = get_array_type(ira->codegen, type_info_declaration_type, declaration_count, false);
2098720998 declaration_array->data.x_array.special = ConstArraySpecialNone;
2098820999 declaration_array->data.x_array.data.s_none.elements = create_const_vals(declaration_count);
2098921000 init_const_slice(ira->codegen, out_val, declaration_array, 0, declaration_count, false);
......@@ -21128,7 +21139,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr
2112821139 ConstExprValue *fn_arg_name_array = create_const_vals(1);
2112921140 fn_arg_name_array->special = ConstValSpecialStatic;
2113021141 fn_arg_name_array->type = get_array_type(ira->codegen,
21131 get_slice_type(ira->codegen, u8_ptr), fn_arg_count);
21142 get_slice_type(ira->codegen, u8_ptr), fn_arg_count, false);
2113221143 fn_arg_name_array->data.x_array.special = ConstArraySpecialNone;
2113321144 fn_arg_name_array->data.x_array.data.s_none.elements = create_const_vals(fn_arg_count);
2113421145
......@@ -21376,7 +21387,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
2137621387 result->special = ConstValSpecialStatic;
2137721388 result->type = ir_type_info_get_type(ira, "Array", nullptr);
2137821389
21379 ConstExprValue **fields = alloc_const_vals_ptrs(2);
21390 ConstExprValue **fields = alloc_const_vals_ptrs(3);
2138021391 result->data.x_struct.fields = fields;
2138121392
2138221393 // len: usize
......@@ -21389,6 +21400,11 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
2138921400 fields[1]->special = ConstValSpecialStatic;
2139021401 fields[1]->type = ira->codegen->builtin_types.entry_type;
2139121402 fields[1]->data.x_type = type_entry->data.array.child_type;
21403 // is_null_terminated: bool
21404 ensure_field_index(result->type, "is_null_terminated", 2);
21405 fields[2]->special = ConstValSpecialStatic;
21406 fields[2]->type = ira->codegen->builtin_types.entry_bool;
21407 fields[2]->data.x_bool = type_entry->data.array.is_null_terminated;
2139221408
2139321409 break;
2139421410 }
......@@ -21476,7 +21492,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
2147621492
2147721493 ConstExprValue *enum_field_array = create_const_vals(1);
2147821494 enum_field_array->special = ConstValSpecialStatic;
21479 enum_field_array->type = get_array_type(ira->codegen, type_info_enum_field_type, enum_field_count);
21495 enum_field_array->type = get_array_type(ira->codegen, type_info_enum_field_type, enum_field_count, false);
2148021496 enum_field_array->data.x_array.special = ConstArraySpecialNone;
2148121497 enum_field_array->data.x_array.data.s_none.elements = create_const_vals(enum_field_count);
2148221498
......@@ -21524,7 +21540,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
2152421540 uint32_t error_count = type_entry->data.error_set.err_count;
2152521541 ConstExprValue *error_array = create_const_vals(1);
2152621542 error_array->special = ConstValSpecialStatic;
21527 error_array->type = get_array_type(ira->codegen, type_info_error_type, error_count);
21543 error_array->type = get_array_type(ira->codegen, type_info_error_type, error_count, false);
2152821544 error_array->data.x_array.special = ConstArraySpecialNone;
2152921545 error_array->data.x_array.data.s_none.elements = create_const_vals(error_count);
2153021546
......@@ -21620,7 +21636,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
2162021636
2162121637 ConstExprValue *union_field_array = create_const_vals(1);
2162221638 union_field_array->special = ConstValSpecialStatic;
21623 union_field_array->type = get_array_type(ira->codegen, type_info_union_field_type, union_field_count);
21639 union_field_array->type = get_array_type(ira->codegen, type_info_union_field_type, union_field_count, false);
2162421640 union_field_array->data.x_array.special = ConstArraySpecialNone;
2162521641 union_field_array->data.x_array.data.s_none.elements = create_const_vals(union_field_count);
2162621642
......@@ -21700,7 +21716,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
2170021716
2170121717 ConstExprValue *struct_field_array = create_const_vals(1);
2170221718 struct_field_array->special = ConstValSpecialStatic;
21703 struct_field_array->type = get_array_type(ira->codegen, type_info_struct_field_type, struct_field_count);
21719 struct_field_array->type = get_array_type(ira->codegen, type_info_struct_field_type, struct_field_count, false);
2170421720 struct_field_array->data.x_array.special = ConstArraySpecialNone;
2170521721 struct_field_array->data.x_array.data.s_none.elements = create_const_vals(struct_field_count);
2170621722
......@@ -21803,7 +21819,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
2180321819
2180421820 ConstExprValue *fn_arg_array = create_const_vals(1);
2180521821 fn_arg_array->special = ConstValSpecialStatic;
21806 fn_arg_array->type = get_array_type(ira->codegen, type_info_fn_arg_type, fn_arg_count);
21822 fn_arg_array->type = get_array_type(ira->codegen, type_info_fn_arg_type, fn_arg_count, false);
2180721823 fn_arg_array->data.x_array.special = ConstArraySpecialNone;
2180821824 fn_arg_array->data.x_array.data.s_none.elements = create_const_vals(fn_arg_count);
2180921825
......@@ -21983,7 +21999,8 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInstruction *instruction, Zi
2198321999 assert(payload->type == ir_type_info_get_type(ira, "Array", nullptr));
2198422000 return get_array_type(ira->codegen,
2198522001 get_const_field_meta_type(ira, payload, "child", 1),
21986 bigint_as_u64(get_const_field_lit_int(ira, payload, "len", 0))
22002 bigint_as_u64(get_const_field_lit_int(ira, payload, "len", 0)),
22003 get_const_field_bool(ira, payload, "is_null_terminated", 2)
2198722004 );
2198822005 case ZigTypeIdComptimeFloat:
2198922006 return ira->codegen->builtin_types.entry_num_lit_float;
......@@ -22366,7 +22383,7 @@ static IrInstruction *ir_analyze_instruction_embed_file(IrAnalyze *ira, IrInstru
2236622383 }
2236722384
2236822385 ZigType *result_type = get_array_type(ira->codegen,
22369 ira->codegen->builtin_types.entry_u8, buf_len(file_contents));
22386 ira->codegen->builtin_types.entry_u8, buf_len(file_contents), false);
2237022387 IrInstruction *result = ir_const(ira, &instruction->base, result_type);
2237122388 init_const_str_lit(ira->codegen, &result->value, file_contents);
2237222389 return result;
......@@ -27581,7 +27598,9 @@ static Error ir_resolve_lazy_raw(AstNode *source_node, ConstExprValue *val) {
2758127598 if ((err = type_resolve(ira->codegen, elem_type, needed_status)))
2758227599 return err;
2758327600 ZigType *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, elem_type,
27584 lazy_slice_type->is_const, lazy_slice_type->is_volatile, PtrLenUnknown, align_bytes,
27601 lazy_slice_type->is_const, lazy_slice_type->is_volatile,
27602 lazy_slice_type->is_null_terminated ? PtrLenNull : PtrLenUnknown,
27603 align_bytes,
2758527604 0, 0, lazy_slice_type->is_allowzero);
2758627605 val->special = ConstValSpecialStatic;
2758727606 assert(val->type->id == ZigTypeIdMetaType);
test/stage1/behavior/if.zig+4
......@@ -81,6 +81,10 @@ test "if prongs cast to expected type instead of peer type resolution" {
8181 var x: i32 = 0;
8282 x = if (f) 1 else 2;
8383 expect(x == 2);
84
85 var b = true;
86 const y: i32 = if (b) 1 else 2;
87 expect(y == 1);
8488 }
8589 };
8690 S.doTheTest(false);
test/stage1/behavior/type.zig+83-64
......@@ -11,103 +11,122 @@ fn testTypes(comptime types: []const type) void {
1111}
1212
1313test "Type.MetaType" {
14 testing.expect(type == @Type(TypeInfo { .Type = undefined }));
15 testTypes([_]type {type});
14 testing.expect(type == @Type(TypeInfo{ .Type = undefined }));
15 testTypes([_]type{type});
1616}
1717
1818test "Type.Void" {
19 testing.expect(void == @Type(TypeInfo { .Void = undefined }));
20 testTypes([_]type {void});
19 testing.expect(void == @Type(TypeInfo{ .Void = undefined }));
20 testTypes([_]type{void});
2121}
2222
2323test "Type.Bool" {
24 testing.expect(bool == @Type(TypeInfo { .Bool = undefined }));
25 testTypes([_]type {bool});
24 testing.expect(bool == @Type(TypeInfo{ .Bool = undefined }));
25 testTypes([_]type{bool});
2626}
2727
2828test "Type.NoReturn" {
29 testing.expect(noreturn == @Type(TypeInfo { .NoReturn = undefined }));
30 testTypes([_]type {noreturn});
29 testing.expect(noreturn == @Type(TypeInfo{ .NoReturn = undefined }));
30 testTypes([_]type{noreturn});
3131}
3232
3333test "Type.Int" {
34 testing.expect(u1 == @Type(TypeInfo { .Int = TypeInfo.Int { .is_signed = false, .bits = 1 } }));
35 testing.expect(i1 == @Type(TypeInfo { .Int = TypeInfo.Int { .is_signed = true, .bits = 1 } }));
36 testing.expect(u8 == @Type(TypeInfo { .Int = TypeInfo.Int { .is_signed = false, .bits = 8 } }));
37 testing.expect(i8 == @Type(TypeInfo { .Int = TypeInfo.Int { .is_signed = true, .bits = 8 } }));
38 testing.expect(u64 == @Type(TypeInfo { .Int = TypeInfo.Int { .is_signed = false, .bits = 64 } }));
39 testing.expect(i64 == @Type(TypeInfo { .Int = TypeInfo.Int { .is_signed = true, .bits = 64 } }));
40 testTypes([_]type {u8,u32,i64});
34 testing.expect(u1 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .is_signed = false, .bits = 1 } }));
35 testing.expect(i1 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .is_signed = true, .bits = 1 } }));
36 testing.expect(u8 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .is_signed = false, .bits = 8 } }));
37 testing.expect(i8 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .is_signed = true, .bits = 8 } }));
38 testing.expect(u64 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .is_signed = false, .bits = 64 } }));
39 testing.expect(i64 == @Type(TypeInfo{ .Int = TypeInfo.Int{ .is_signed = true, .bits = 64 } }));
40 testTypes([_]type{ u8, u32, i64 });
4141}
4242
4343test "Type.Float" {
44 testing.expect(f16 == @Type(TypeInfo { .Float = TypeInfo.Float { .bits = 16 } }));
45 testing.expect(f32 == @Type(TypeInfo { .Float = TypeInfo.Float { .bits = 32 } }));
46 testing.expect(f64 == @Type(TypeInfo { .Float = TypeInfo.Float { .bits = 64 } }));
47 testing.expect(f128 == @Type(TypeInfo { .Float = TypeInfo.Float { .bits = 128 } }));
48 testTypes([_]type {f16, f32, f64, f128});
44 testing.expect(f16 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 16 } }));
45 testing.expect(f32 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 32 } }));
46 testing.expect(f64 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 64 } }));
47 testing.expect(f128 == @Type(TypeInfo{ .Float = TypeInfo.Float{ .bits = 128 } }));
48 testTypes([_]type{ f16, f32, f64, f128 });
4949}
5050
5151test "Type.Pointer" {
52 testTypes([_]type {
52 testTypes([_]type{
5353 // One Value Pointer Types
54 *u8, *const u8,
55 *volatile u8, *const volatile u8,
56 *align(4) u8, *const align(4) u8,
57 *volatile align(4) u8, *const volatile align(4) u8,
58 *align(8) u8, *const align(8) u8,
59 *volatile align(8) u8, *const volatile align(8) u8,
60 *allowzero u8, *const allowzero u8,
61 *volatile allowzero u8, *const volatile allowzero u8,
62 *align(4) allowzero u8, *const align(4) allowzero u8,
63 *volatile align(4) allowzero u8, *const volatile align(4) allowzero u8,
54 *u8, *const u8,
55 *volatile u8, *const volatile u8,
56 *align(4) u8, *align(4) const u8,
57 *align(4) volatile u8, *align(4) const volatile u8,
58 *align(8) u8, *align(8) const u8,
59 *align(8) volatile u8, *align(8) const volatile u8,
60 *allowzero u8, *allowzero const u8,
61 *allowzero volatile u8, *allowzero const volatile u8,
62 *allowzero align(4) u8, *allowzero align(4) const u8,
63 *allowzero align(4) volatile u8, *allowzero align(4) const volatile u8,
6464 // Many Values Pointer Types
65 [*]u8, [*]const u8,
66 [*]volatile u8, [*]const volatile u8,
67 [*]align(4) u8, [*]const align(4) u8,
68 [*]volatile align(4) u8, [*]const volatile align(4) u8,
69 [*]align(8) u8, [*]const align(8) u8,
70 [*]volatile align(8) u8, [*]const volatile align(8) u8,
71 [*]allowzero u8, [*]const allowzero u8,
72 [*]volatile allowzero u8, [*]const volatile allowzero u8,
73 [*]align(4) allowzero u8, [*]const align(4) allowzero u8,
74 [*]volatile align(4) allowzero u8, [*]const volatile align(4) allowzero u8,
65 [*]u8, [*]const u8,
66 [*]volatile u8, [*]const volatile u8,
67 [*]align(4) u8, [*]align(4) const u8,
68 [*]align(4) volatile u8, [*]align(4) const volatile u8,
69 [*]align(8) u8, [*]align(8) const u8,
70 [*]align(8) volatile u8, [*]align(8) const volatile u8,
71 [*]allowzero u8, [*]allowzero const u8,
72 [*]allowzero volatile u8, [*]allowzero const volatile u8,
73 [*]allowzero align(4) u8, [*]allowzero align(4) const u8,
74 [*]allowzero align(4) volatile u8, [*]allowzero align(4) const volatile u8,
7575 // Slice Types
76 []u8, []const u8,
77 []volatile u8, []const volatile u8,
78 []align(4) u8, []const align(4) u8,
79 []volatile align(4) u8, []const volatile align(4) u8,
80 []align(8) u8, []const align(8) u8,
81 []volatile align(8) u8, []const volatile align(8) u8,
82 []allowzero u8, []const allowzero u8,
83 []volatile allowzero u8, []const volatile allowzero u8,
84 []align(4) allowzero u8, []const align(4) allowzero u8,
85 []volatile align(4) allowzero u8, []const volatile align(4) allowzero u8,
76 []u8, []const u8,
77 []volatile u8, []const volatile u8,
78 []align(4) u8, []align(4) const u8,
79 []align(4) volatile u8, []align(4) const volatile u8,
80 []align(8) u8, []align(8) const u8,
81 []align(8) volatile u8, []align(8) const volatile u8,
82 []allowzero u8, []allowzero const u8,
83 []allowzero volatile u8, []allowzero const volatile u8,
84 []allowzero align(4) u8, []allowzero align(4) const u8,
85 []allowzero align(4) volatile u8, []allowzero align(4) const volatile u8,
8686 // C Pointer Types
87 [*c]u8, [*c]const u8,
88 [*c]volatile u8, [*c]const volatile u8,
89 [*c]align(4) u8, [*c]const align(4) u8,
90 [*c]volatile align(4) u8, [*c]const volatile align(4) u8,
91 [*c]align(8) u8, [*c]const align(8) u8,
92 [*c]volatile align(8) u8, [*c]const volatile align(8) u8,
87 [*c]u8, [*c]const u8,
88 [*c]volatile u8, [*c]const volatile u8,
89 [*c]align(4) u8, [*c]align(4) const u8,
90 [*c]align(4) volatile u8, [*c]align(4) const volatile u8,
91 [*c]align(8) u8, [*c]align(8) const u8,
92 [*c]align(8) volatile u8, [*c]align(8) const volatile u8,
9393 });
9494}
9595
9696test "Type.Array" {
97 testing.expect([123]u8 == @Type(TypeInfo { .Array = TypeInfo.Array { .len = 123, .child = u8 } }));
98 testing.expect([2]u32 == @Type(TypeInfo { .Array = TypeInfo.Array { .len = 2, .child = u32 } }));
99 testTypes([_]type {[1]u8, [30]usize, [7]bool});
97 testing.expect([123]u8 == @Type(TypeInfo{
98 .Array = TypeInfo.Array{
99 .len = 123,
100 .child = u8,
101 .is_null_terminated = false,
102 },
103 }));
104 testing.expect([2]u32 == @Type(TypeInfo{
105 .Array = TypeInfo.Array{
106 .len = 2,
107 .child = u32,
108 .is_null_terminated = false,
109 },
110 }));
111 testing.expect([2]null u32 == @Type(TypeInfo{
112 .Array = TypeInfo.Array{
113 .len = 2,
114 .child = u32,
115 .is_null_terminated = true,
116 },
117 }));
118 testTypes([_]type{ [1]u8, [30]usize, [7]bool });
100119}
101120
102121test "Type.ComptimeFloat" {
103 testTypes([_]type {comptime_float});
122 testTypes([_]type{comptime_float});
104123}
105124test "Type.ComptimeInt" {
106 testTypes([_]type {comptime_int});
125 testTypes([_]type{comptime_int});
107126}
108127test "Type.Undefined" {
109 testTypes([_]type {@typeOf(undefined)});
128 testTypes([_]type{@typeOf(undefined)});
110129}
111130test "Type.Null" {
112 testTypes([_]type {@typeOf(null)});
131 testTypes([_]type{@typeOf(null)});
113132}
test/stage1/behavior/type_info.zig+26-5
......@@ -46,6 +46,7 @@ fn testPointer() void {
4646 expect(u32_ptr_info.Pointer.is_volatile == false);
4747 expect(u32_ptr_info.Pointer.alignment == @alignOf(u32));
4848 expect(u32_ptr_info.Pointer.child == u32);
49 expect(u32_ptr_info.Pointer.is_null_terminated == false);
4950}
5051
5152test "type info: unknown length pointer type info" {
......@@ -55,14 +56,34 @@ test "type info: unknown length pointer type info" {
5556
5657fn testUnknownLenPtr() void {
5758 const u32_ptr_info = @typeInfo([*]const volatile f64);
58 expect(@as(TypeId,u32_ptr_info) == TypeId.Pointer);
59 expect(@as(TypeId, u32_ptr_info) == TypeId.Pointer);
5960 expect(u32_ptr_info.Pointer.size == TypeInfo.Pointer.Size.Many);
6061 expect(u32_ptr_info.Pointer.is_const == true);
6162 expect(u32_ptr_info.Pointer.is_volatile == true);
63 expect(u32_ptr_info.Pointer.is_null_terminated == false);
6264 expect(u32_ptr_info.Pointer.alignment == @alignOf(f64));
6365 expect(u32_ptr_info.Pointer.child == f64);
6466}
6567
68test "type info: null terminated pointer type info" {
69 testNullTerminatedPtr();
70 comptime testNullTerminatedPtr();
71}
72
73fn testNullTerminatedPtr() void {
74 const ptr_info = @typeInfo([*]null u8);
75 expect(@as(TypeId, ptr_info) == TypeId.Pointer);
76 expect(ptr_info.Pointer.size == TypeInfo.Pointer.Size.Many);
77 expect(ptr_info.Pointer.is_const == false);
78 expect(ptr_info.Pointer.is_volatile == false);
79 expect(ptr_info.Pointer.is_null_terminated == true);
80
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);
85}
86
6687test "type info: C pointer type info" {
6788 testCPtr();
6889 comptime testCPtr();
......@@ -70,7 +91,7 @@ test "type info: C pointer type info" {
7091
7192fn testCPtr() void {
7293 const ptr_info = @typeInfo([*c]align(4) const i8);
73 expect(@as(TypeId,ptr_info) == TypeId.Pointer);
94 expect(@as(TypeId, ptr_info) == TypeId.Pointer);
7495 expect(ptr_info.Pointer.size == TypeInfo.Pointer.Size.C);
7596 expect(ptr_info.Pointer.is_const);
7697 expect(!ptr_info.Pointer.is_volatile);
......@@ -288,13 +309,13 @@ test "type info: anyframe and anyframe->T" {
288309fn testAnyFrame() void {
289310 {
290311 const anyframe_info = @typeInfo(anyframe->i32);
291 expect(@as(TypeId,anyframe_info) == .AnyFrame);
312 expect(@as(TypeId, anyframe_info) == .AnyFrame);
292313 expect(anyframe_info.AnyFrame.child.? == i32);
293314 }
294315
295316 {
296317 const anyframe_info = @typeInfo(anyframe);
297 expect(@as(TypeId,anyframe_info) == .AnyFrame);
318 expect(@as(TypeId, anyframe_info) == .AnyFrame);
298319 expect(anyframe_info.AnyFrame.child == null);
299320 }
300321}
......@@ -334,7 +355,7 @@ test "type info: extern fns with and without lib names" {
334355 if (std.mem.eql(u8, decl.name, "bar1")) {
335356 expect(decl.data.Fn.lib_name == null);
336357 } else {
337 std.testing.expectEqual(@as([]const u8,"cool"), decl.data.Fn.lib_name.?);
358 std.testing.expectEqual(@as([]const u8, "cool"), decl.data.Fn.lib_name.?);
338359 }
339360 }
340361 }