| ... | ... | @@ -740,14 +740,24 @@ pub const DeclGen = struct { |
| 740 | 740 | try writer.writeByte(')'); |
| 741 | 741 | } |
| 742 | 742 | |
| 743 | | try writer.writeByte('{'); |
| 744 | | const c_len = ty.arrayLenIncludingSentinel(); |
| 745 | | var index: usize = 0; |
| 746 | | while (index < c_len) : (index += 1) { |
| 747 | | if (index > 0) try writer.writeAll(", "); |
| 748 | | try dg.renderValue(writer, ty.childType(), val, .Initializer); |
| 743 | const ai = ty.arrayInfo(); |
| 744 | if (ai.elem_type.eql(Type.u8, dg.module)) { |
| 745 | try writer.writeByte('"'); |
| 746 | const c_len = ty.arrayLenIncludingSentinel(); |
| 747 | var index: usize = 0; |
| 748 | while (index < c_len) : (index += 1) |
| 749 | try writeStringLiteralChar(writer, 0xaa); |
| 750 | return writer.writeByte('"'); |
| 751 | } else { |
| 752 | try writer.writeByte('{'); |
| 753 | const c_len = ty.arrayLenIncludingSentinel(); |
| 754 | var index: usize = 0; |
| 755 | while (index < c_len) : (index += 1) { |
| 756 | if (index > 0) try writer.writeAll(", "); |
| 757 | try dg.renderValue(writer, ty.childType(), val, .Initializer); |
| 758 | } |
| 759 | return writer.writeByte('}'); |
| 749 | 760 | } |
| 750 | | return writer.writeByte('}'); |
| 751 | 761 | }, |
| 752 | 762 | .ComptimeInt, |
| 753 | 763 | .ComptimeFloat, |
| ... | ... | @@ -931,25 +941,48 @@ pub const DeclGen = struct { |
| 931 | 941 | } |
| 932 | 942 | try writer.writeByte('}'); |
| 933 | 943 | }, |
| 944 | .bytes => { |
| 945 | try writer.print("{s}", .{fmtStringLiteral(val.castTag(.bytes).?.data)}); |
| 946 | }, |
| 947 | .str_lit => { |
| 948 | const str_lit = val.castTag(.str_lit).?.data; |
| 949 | const bytes = dg.module.string_literal_bytes.items[str_lit.index..][0..str_lit.len]; |
| 950 | try writer.print("{s}", .{fmtStringLiteral(bytes)}); |
| 951 | }, |
| 934 | 952 | else => { |
| 935 | 953 | // Fall back to generic implementation. |
| 936 | 954 | var arena = std.heap.ArenaAllocator.init(dg.gpa); |
| 937 | 955 | defer arena.deinit(); |
| 938 | 956 | const arena_allocator = arena.allocator(); |
| 939 | 957 | |
| 940 | | try writer.writeByte('{'); |
| 941 | 958 | const ai = ty.arrayInfo(); |
| 942 | | var index: usize = 0; |
| 943 | | while (index < ai.len) : (index += 1) { |
| 944 | | if (index != 0) try writer.writeByte(','); |
| 945 | | const elem_val = try val.elemValue(dg.module, arena_allocator, index); |
| 946 | | try dg.renderValue(writer, ai.elem_type, elem_val, .Initializer); |
| 947 | | } |
| 948 | | if (ai.sentinel) |s| { |
| 949 | | if (index != 0) try writer.writeByte(','); |
| 950 | | try dg.renderValue(writer, ai.elem_type, s, .Initializer); |
| 959 | if (ai.elem_type.eql(Type.u8, dg.module)) { |
| 960 | try writer.writeByte('"'); |
| 961 | var index: usize = 0; |
| 962 | while (index < ai.len) : (index += 1) { |
| 963 | const elem_val = try val.elemValue(dg.module, arena_allocator, index); |
| 964 | const elem_val_u8 = @intCast(u8, elem_val.toUnsignedInt(target)); |
| 965 | try writeStringLiteralChar(writer, elem_val_u8); |
| 966 | } |
| 967 | if (ai.sentinel) |s| { |
| 968 | const s_u8 = @intCast(u8, s.toUnsignedInt(target)); |
| 969 | try writeStringLiteralChar(writer, s_u8); |
| 970 | } |
| 971 | try writer.writeByte('"'); |
| 972 | } else { |
| 973 | try writer.writeByte('{'); |
| 974 | var index: usize = 0; |
| 975 | while (index < ai.len) : (index += 1) { |
| 976 | if (index != 0) try writer.writeByte(','); |
| 977 | const elem_val = try val.elemValue(dg.module, arena_allocator, index); |
| 978 | try dg.renderValue(writer, ai.elem_type, elem_val, .Initializer); |
| 979 | } |
| 980 | if (ai.sentinel) |s| { |
| 981 | if (index != 0) try writer.writeByte(','); |
| 982 | try dg.renderValue(writer, ai.elem_type, s, .Initializer); |
| 983 | } |
| 984 | try writer.writeByte('}'); |
| 951 | 985 | } |
| 952 | | try writer.writeByte('}'); |
| 953 | 986 | }, |
| 954 | 987 | } |
| 955 | 988 | }, |
| ... | ... | @@ -5618,7 +5651,17 @@ fn formatStringLiteral( |
| 5618 | 5651 | ) @TypeOf(writer).Error!void { |
| 5619 | 5652 | if (fmt.len != 1 or fmt[0] != 's') @compileError("Invalid fmt: " ++ fmt); |
| 5620 | 5653 | try writer.writeByte('\"'); |
| 5621 | | for (str) |c| switch (c) { |
| 5654 | for (str) |c| |
| 5655 | try writeStringLiteralChar(writer, c); |
| 5656 | try writer.writeByte('\"'); |
| 5657 | } |
| 5658 | |
| 5659 | fn fmtStringLiteral(str: []const u8) std.fmt.Formatter(formatStringLiteral) { |
| 5660 | return .{ .data = str }; |
| 5661 | } |
| 5662 | |
| 5663 | fn writeStringLiteralChar(writer: anytype, c: u8) !void { |
| 5664 | switch (c) { |
| 5622 | 5665 | 7 => try writer.writeAll("\\a"), |
| 5623 | 5666 | 8 => try writer.writeAll("\\b"), |
| 5624 | 5667 | '\t' => try writer.writeAll("\\t"), |
| ... | ... | @@ -5631,11 +5674,7 @@ fn formatStringLiteral( |
| 5631 | 5674 | ' '...'~' => try writer.writeByte(c), |
| 5632 | 5675 | else => try writer.print("\\{o:0>3}", .{c}), |
| 5633 | 5676 | }, |
| 5634 | | }; |
| 5635 | | try writer.writeByte('\"'); |
| 5636 | | } |
| 5637 | | fn fmtStringLiteral(str: []const u8) std.fmt.Formatter(formatStringLiteral) { |
| 5638 | | return .{ .data = str }; |
| 5677 | } |
| 5639 | 5678 | } |
| 5640 | 5679 | |
| 5641 | 5680 | fn undefPattern(comptime IntType: type) IntType { |