authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-09-22 15:28:37+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-10-17 10:27:19+03:00
log2c294676b52f2ba62172fa778a198f92d6a969f0
treec91693aa067cb821b480b4ba3cad5caf533e3ce2
parent8d38a91ca8b52d8e209db5041bd6f351da9cac22
signaturelock-open Commit is signed but in an unrecognized format.

use new format specifier in translate-c and std lib


2 files changed, 9 insertions(+), 69 deletions(-)

lib/std/build.zig+6-6
...@@ -1767,7 +1767,7 @@ pub const LibExeObjStep = struct {...@@ -1767,7 +1767,7 @@ pub const LibExeObjStep = struct {
1767 const out = self.build_options_contents.outStream();1767 const out = self.build_options_contents.outStream();
1768 switch (T) {1768 switch (T) {
1769 []const []const u8 => {1769 []const []const u8 => {
1770 out.print("pub const {}: []const []const u8 = &[_][]const u8{{\n", .{name}) catch unreachable;1770 out.print("pub const {z}: []const []const u8 = &[_][]const u8{{\n", .{name}) catch unreachable;
1771 for (value) |slice| {1771 for (value) |slice| {
1772 out.writeAll(" ") catch unreachable;1772 out.writeAll(" ") catch unreachable;
1773 std.zig.renderStringLiteral(slice, out) catch unreachable;1773 std.zig.renderStringLiteral(slice, out) catch unreachable;
...@@ -1777,13 +1777,13 @@ pub const LibExeObjStep = struct {...@@ -1777,13 +1777,13 @@ pub const LibExeObjStep = struct {
1777 return;1777 return;
1778 },1778 },
1779 []const u8 => {1779 []const u8 => {
1780 out.print("pub const {}: []const u8 = ", .{name}) catch unreachable;1780 out.print("pub const {z}: []const u8 = ", .{name}) catch unreachable;
1781 std.zig.renderStringLiteral(value, out) catch unreachable;1781 std.zig.renderStringLiteral(value, out) catch unreachable;
1782 out.writeAll(";\n") catch unreachable;1782 out.writeAll(";\n") catch unreachable;
1783 return;1783 return;
1784 },1784 },
1785 ?[]const u8 => {1785 ?[]const u8 => {
1786 out.print("pub const {}: ?[]const u8 = ", .{name}) catch unreachable;1786 out.print("pub const {z}: ?[]const u8 = ", .{name}) catch unreachable;
1787 if (value) |payload| {1787 if (value) |payload| {
1788 std.zig.renderStringLiteral(payload, out) catch unreachable;1788 std.zig.renderStringLiteral(payload, out) catch unreachable;
1789 out.writeAll(";\n") catch unreachable;1789 out.writeAll(";\n") catch unreachable;
...@@ -1796,15 +1796,15 @@ pub const LibExeObjStep = struct {...@@ -1796,15 +1796,15 @@ pub const LibExeObjStep = struct {
1796 }1796 }
1797 switch (@typeInfo(T)) {1797 switch (@typeInfo(T)) {
1798 .Enum => |enum_info| {1798 .Enum => |enum_info| {
1799 out.print("pub const {} = enum {{\n", .{@typeName(T)}) catch unreachable;1799 out.print("pub const {z} = enum {{\n", .{@typeName(T)}) catch unreachable;
1800 inline for (enum_info.fields) |field| {1800 inline for (enum_info.fields) |field| {
1801 out.print(" {},\n", .{field.name}) catch unreachable;1801 out.print(" {z},\n", .{field.name}) catch unreachable;
1802 }1802 }
1803 out.writeAll("};\n") catch unreachable;1803 out.writeAll("};\n") catch unreachable;
1804 },1804 },
1805 else => {},1805 else => {},
1806 }1806 }
1807 out.print("pub const {} = {};\n", .{ name, value }) catch unreachable;1807 out.print("pub const {z} = {};\n", .{ name, value }) catch unreachable;
1808 }1808 }
18091809
1810 /// The value is the path in the cache dir.1810 /// The value is the path in the cache dir.
src/translate_c.zig+3-63
...@@ -1972,16 +1972,7 @@ fn transStringLiteral(...@@ -1972,16 +1972,7 @@ fn transStringLiteral(
1972 const bytes_ptr = stmt.getString_bytes_begin_size(&len);1972 const bytes_ptr = stmt.getString_bytes_begin_size(&len);
1973 const str = bytes_ptr[0..len];1973 const str = bytes_ptr[0..len];
19741974
1975 var char_buf: [4]u8 = undefined;1975 const token = try appendTokenFmt(rp.c, .StringLiteral, "\"{Z}\"", .{str});
1976 len = 0;
1977 for (str) |c| len += escapeChar(c, &char_buf).len;
1978
1979 const buf = try rp.c.arena.alloc(u8, len + "\"\"".len);
1980 buf[0] = '"';
1981 writeEscapedString(buf[1..], str);
1982 buf[buf.len - 1] = '"';
1983
1984 const token = try appendToken(rp.c, .StringLiteral, buf);
1985 const node = try rp.c.arena.create(ast.Node.OneToken);1976 const node = try rp.c.arena.create(ast.Node.OneToken);
1986 node.* = .{1977 node.* = .{
1987 .base = .{ .tag = .StringLiteral },1978 .base = .{ .tag = .StringLiteral },
...@@ -1999,41 +1990,6 @@ fn transStringLiteral(...@@ -1999,41 +1990,6 @@ fn transStringLiteral(
1999 }1990 }
2000}1991}
20011992
2002fn escapedStringLen(s: []const u8) usize {
2003 var len: usize = 0;
2004 var char_buf: [4]u8 = undefined;
2005 for (s) |c| len += escapeChar(c, &char_buf).len;
2006 return len;
2007}
2008
2009fn writeEscapedString(buf: []u8, s: []const u8) void {
2010 var char_buf: [4]u8 = undefined;
2011 var i: usize = 0;
2012 for (s) |c| {
2013 const escaped = escapeChar(c, &char_buf);
2014 mem.copy(u8, buf[i..], escaped);
2015 i += escaped.len;
2016 }
2017}
2018
2019// Returns either a string literal or a slice of `buf`.
2020fn escapeChar(c: u8, char_buf: *[4]u8) []const u8 {
2021 return switch (c) {
2022 '\"' => "\\\"",
2023 '\'' => "\\'",
2024 '\\' => "\\\\",
2025 '\n' => "\\n",
2026 '\r' => "\\r",
2027 '\t' => "\\t",
2028 // Handle the remaining escapes Zig doesn't support by turning them
2029 // into their respective hex representation
2030 else => if (std.ascii.isCntrl(c))
2031 std.fmt.bufPrint(char_buf, "\\x{x:0>2}", .{c}) catch unreachable
2032 else
2033 std.fmt.bufPrint(char_buf, "{c}", .{c}) catch unreachable,
2034 };
2035}
2036
2037fn transCCast(1993fn transCCast(
2038 rp: RestorePoint,1994 rp: RestorePoint,
2039 scope: *Scope,1995 scope: *Scope,
...@@ -2922,8 +2878,7 @@ fn transCharLiteral(...@@ -2922,8 +2878,7 @@ fn transCharLiteral(
2922 if (val > 255)2878 if (val > 255)
2923 break :blk try transCreateNodeInt(rp.c, val);2879 break :blk try transCreateNodeInt(rp.c, val);
2924 }2880 }
2925 var char_buf: [4]u8 = undefined;2881 const token = try appendTokenFmt(rp.c, .CharLiteral, "'{Z}'", .{@intCast(u8, val)});
2926 const token = try appendTokenFmt(rp.c, .CharLiteral, "'{}'", .{escapeChar(@intCast(u8, val), &char_buf)});
2927 const node = try rp.c.arena.create(ast.Node.OneToken);2882 const node = try rp.c.arena.create(ast.Node.OneToken);
2928 node.* = .{2883 node.* = .{
2929 .base = .{ .tag = .CharLiteral },2884 .base = .{ .tag = .CharLiteral },
...@@ -5247,23 +5202,8 @@ fn isZigPrimitiveType(name: []const u8) bool {...@@ -5247,23 +5202,8 @@ fn isZigPrimitiveType(name: []const u8) bool {
5247 mem.eql(u8, name, "c_ulonglong");5202 mem.eql(u8, name, "c_ulonglong");
5248}5203}
52495204
5250fn isValidZigIdentifier(name: []const u8) bool {
5251 for (name) |c, i| {
5252 switch (c) {
5253 '_', 'a'...'z', 'A'...'Z' => {},
5254 '0'...'9' => if (i == 0) return false,
5255 else => return false,
5256 }
5257 }
5258 return true;
5259}
5260
5261fn appendIdentifier(c: *Context, name: []const u8) !ast.TokenIndex {5205fn appendIdentifier(c: *Context, name: []const u8) !ast.TokenIndex {
5262 if (!isValidZigIdentifier(name) or std.zig.Token.getKeyword(name) != null) {5206 return appendTokenFmt(c, .Identifier, "{z}", .{name});
5263 return appendTokenFmt(c, .Identifier, "@\"{}\"", .{name});
5264 } else {
5265 return appendTokenFmt(c, .Identifier, "{}", .{name});
5266 }
5267}5207}
52685208
5269fn transCreateNodeIdentifier(c: *Context, name: []const u8) !*ast.Node {5209fn transCreateNodeIdentifier(c: *Context, name: []const u8) !*ast.Node {