authorgravatar for benjamin.feng@glassdoor.comBenjamin Feng <benjamin.feng@glassdoor.com> 2020-03-06 09:33:12-06:00
committergravatar for benjamin.feng@glassdoor.comBenjamin Feng <benjamin.feng@glassdoor.com> 2020-03-12 10:41:09-05:00
log4023ed56d4fd7b627aed73428b5fa4be4b7c05ad
treef9af2b1886a98e91d72c5c67ccf1138dac26180d
parent8dd078b99a31f49194c07af93b0f555f2c7260dc

Convert translate-c to fmtstream


1 files changed, 23 insertions(+), 28 deletions(-)

src-self-hosted/translate_c.zig+23-28
......@@ -89,7 +89,7 @@ const Scope = struct {
8989 var proposed_name = name;
9090 while (scope.contains(proposed_name)) {
9191 scope.mangle_count += 1;
92 proposed_name = try std.fmt.allocPrint(c.a(), "{}_{}", .{ name, scope.mangle_count });
92 proposed_name = try std.fmtstream.allocPrint(c.a(), "{}_{}", .{ name, scope.mangle_count });
9393 }
9494 try scope.variables.push(.{ .name = name, .alias = proposed_name });
9595 return proposed_name;
......@@ -246,7 +246,7 @@ pub const Context = struct {
246246
247247 const line = ZigClangSourceManager_getSpellingLineNumber(c.source_manager, spelling_loc);
248248 const column = ZigClangSourceManager_getSpellingColumnNumber(c.source_manager, spelling_loc);
249 return std.fmt.allocPrint(c.a(), "{}:{}:{}", .{ filename, line, column });
249 return std.fmtstream.allocPrint(c.a(), "{}:{}:{}", .{ filename, line, column });
250250 }
251251};
252252
......@@ -516,7 +516,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {
516516
517517 const arg_name = blk: {
518518 const param_prefix = if (is_const) "" else "arg_";
519 const bare_arg_name = try std.fmt.allocPrint(c.a(), "{}{}", .{ param_prefix, mangled_param_name });
519 const bare_arg_name = try std.fmtstream.allocPrint(c.a(), "{}{}", .{ param_prefix, mangled_param_name });
520520 break :blk try block_scope.makeMangledName(c, bare_arg_name);
521521 };
522522
......@@ -560,7 +560,7 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {
560560
561561 // TODO https://github.com/ziglang/zig/issues/3756
562562 // TODO https://github.com/ziglang/zig/issues/1802
563 const checked_name = if (isZigPrimitiveType(var_name)) try std.fmt.allocPrint(c.a(), "{}_{}", .{var_name, c.getMangle()}) else var_name;
563 const checked_name = if (isZigPrimitiveType(var_name)) try std.fmtstream.allocPrint(c.a(), "{}_{}", .{var_name, c.getMangle()}) else var_name;
564564 const var_decl_loc = ZigClangVarDecl_getLocation(var_decl);
565565
566566 const qual_type = ZigClangVarDecl_getTypeSourceInfo_getType(var_decl);
......@@ -620,7 +620,7 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {
620620 _ = try appendToken(rp.c, .LParen, "(");
621621 const expr = try transCreateNodeStringLiteral(
622622 rp.c,
623 try std.fmt.allocPrint(rp.c.a(), "\"{}\"", .{str_ptr[0..str_len]}),
623 try std.fmtstream.allocPrint(rp.c.a(), "\"{}\"", .{str_ptr[0..str_len]}),
624624 );
625625 _ = try appendToken(rp.c, .RParen, ")");
626626
......@@ -677,7 +677,7 @@ fn transTypeDef(c: *Context, typedef_decl: *const ZigClangTypedefNameDecl, top_l
677677
678678 // TODO https://github.com/ziglang/zig/issues/3756
679679 // TODO https://github.com/ziglang/zig/issues/1802
680 const checked_name = if (isZigPrimitiveType(typedef_name)) try std.fmt.allocPrint(c.a(), "{}_{}", .{typedef_name, c.getMangle()}) else typedef_name;
680 const checked_name = if (isZigPrimitiveType(typedef_name)) try std.fmtstream.allocPrint(c.a(), "{}_{}", .{typedef_name, c.getMangle()}) else typedef_name;
681681
682682 if (mem.eql(u8, checked_name, "uint8_t"))
683683 return transTypeDefAsBuiltin(c, typedef_decl, "u8")
......@@ -738,7 +738,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
738738 // Record declarations such as `struct {...} x` have no name but they're not
739739 // anonymous hence here isAnonymousStructOrUnion is not needed
740740 if (bare_name.len == 0) {
741 bare_name = try std.fmt.allocPrint(c.a(), "unnamed_{}", .{c.getMangle()});
741 bare_name = try std.fmtstream.allocPrint(c.a(), "unnamed_{}", .{c.getMangle()});
742742 is_unnamed = true;
743743 }
744744
......@@ -755,7 +755,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
755755 return null;
756756 }
757757
758 const name = try std.fmt.allocPrint(c.a(), "{}_{}", .{ container_kind_name, bare_name });
758 const name = try std.fmtstream.allocPrint(c.a(), "{}_{}", .{ container_kind_name, bare_name });
759759 _ = try c.decl_table.put(@ptrToInt(ZigClangRecordDecl_getCanonicalDecl(record_decl)), name);
760760
761761 const node = try transCreateNodeVarDecl(c, !is_unnamed, true, name);
......@@ -812,7 +812,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!?*
812812 var is_anon = false;
813813 var raw_name = try c.str(ZigClangNamedDecl_getName_bytes_begin(@ptrCast(*const ZigClangNamedDecl, field_decl)));
814814 if (ZigClangFieldDecl_isAnonymousStructOrUnion(field_decl)) {
815 raw_name = try std.fmt.allocPrint(c.a(), "unnamed_{}", .{c.getMangle()});
815 raw_name = try std.fmtstream.allocPrint(c.a(), "unnamed_{}", .{c.getMangle()});
816816 is_anon = true;
817817 }
818818 const field_name = try appendIdentifier(c, raw_name);
......@@ -882,11 +882,11 @@ fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.No
882882 var bare_name = try c.str(ZigClangNamedDecl_getName_bytes_begin(@ptrCast(*const ZigClangNamedDecl, enum_decl)));
883883 var is_unnamed = false;
884884 if (bare_name.len == 0) {
885 bare_name = try std.fmt.allocPrint(c.a(), "unnamed_{}", .{c.getMangle()});
885 bare_name = try std.fmtstream.allocPrint(c.a(), "unnamed_{}", .{c.getMangle()});
886886 is_unnamed = true;
887887 }
888888
889 const name = try std.fmt.allocPrint(c.a(), "enum_{}", .{bare_name});
889 const name = try std.fmtstream.allocPrint(c.a(), "enum_{}", .{bare_name});
890890 _ = try c.decl_table.put(@ptrToInt(ZigClangEnumDecl_getCanonicalDecl(enum_decl)), name);
891891 const node = try transCreateNodeVarDecl(c, !is_unnamed, true, name);
892892 node.eq_token = try appendToken(c, .Equal, "=");
......@@ -1754,9 +1754,9 @@ fn escapeChar(c: u8, char_buf: *[4]u8) []const u8 {
17541754 // Handle the remaining escapes Zig doesn't support by turning them
17551755 // into their respective hex representation
17561756 if (std.ascii.isCntrl(c))
1757 return std.fmt.bufPrint(char_buf[0..], "\\x{x:0<2}", .{c}) catch unreachable
1757 return std.fmtstream.bufPrint(char_buf[0..], "\\x{x:0<2}", .{c}) catch unreachable
17581758 else
1759 return std.fmt.bufPrint(char_buf[0..], "{c}", .{c}) catch unreachable;
1759 return std.fmtstream.bufPrint(char_buf[0..], "{c}", .{c}) catch unreachable;
17601760 },
17611761 };
17621762}
......@@ -2436,7 +2436,7 @@ fn transCase(
24362436) TransError!*ast.Node {
24372437 const block_scope = scope.findBlockScope(rp.c) catch unreachable;
24382438 const switch_scope = scope.getSwitch();
2439 const label = try std.fmt.allocPrint(rp.c.a(), "__case_{}", .{switch_scope.cases.len - @boolToInt(switch_scope.has_default)});
2439 const label = try std.fmtstream.allocPrint(rp.c.a(), "__case_{}", .{switch_scope.cases.len - @boolToInt(switch_scope.has_default)});
24402440 _ = try appendToken(rp.c, .Semicolon, ";");
24412441
24422442 const expr = if (ZigClangCaseStmt_getRHS(stmt)) |rhs| blk: {
......@@ -4607,7 +4607,7 @@ fn finishTransFnProto(
46074607 _ = try appendToken(rp.c, .LParen, "(");
46084608 const expr = try transCreateNodeStringLiteral(
46094609 rp.c,
4610 try std.fmt.allocPrint(rp.c.a(), "\"{}\"", .{str_ptr[0..str_len]}),
4610 try std.fmtstream.allocPrint(rp.c.a(), "\"{}\"", .{str_ptr[0..str_len]}),
46114611 );
46124612 _ = try appendToken(rp.c, .RParen, ")");
46134613
......@@ -4752,15 +4752,10 @@ fn appendToken(c: *Context, token_id: Token.Id, bytes: []const u8) !ast.TokenInd
47524752}
47534753
47544754fn appendTokenFmt(c: *Context, token_id: Token.Id, comptime format: []const u8, args: var) !ast.TokenIndex {
4755 const S = struct {
4756 fn callback(context: *Context, bytes: []const u8) error{OutOfMemory}!void {
4757 return context.source_buffer.append(bytes);
4758 }
4759 };
47604755 const start_index = c.source_buffer.len();
47614756 errdefer c.source_buffer.shrink(start_index);
47624757
4763 try std.fmt.format(c, error{OutOfMemory}, S.callback, format, args);
4758 try c.source_buffer.print(format, args);
47644759 const end_index = c.source_buffer.len();
47654760 const token_index = c.tree.tokens.len;
47664761 const new_token = try c.tree.tokens.addOne();
......@@ -4871,7 +4866,7 @@ fn transPreprocessorEntities(c: *Context, unit: *ZigClangASTUnit) Error!void {
48714866 const name = try c.str(raw_name);
48724867 // TODO https://github.com/ziglang/zig/issues/3756
48734868 // TODO https://github.com/ziglang/zig/issues/1802
4874 const mangled_name = if (isZigPrimitiveType(name)) try std.fmt.allocPrint(c.a(), "{}_{}", .{name, c.getMangle()}) else name;
4869 const mangled_name = if (isZigPrimitiveType(name)) try std.fmtstream.allocPrint(c.a(), "{}_{}", .{name, c.getMangle()}) else name;
48754870 if (scope.containsNow(mangled_name)) {
48764871 continue;
48774872 }
......@@ -5156,11 +5151,11 @@ fn parseCNumLit(c: *Context, tok: *CToken, source: []const u8, source_loc: ZigCl
51565151 switch (lit_bytes[1]) {
51575152 '0'...'7' => {
51585153 // Octal
5159 lit_bytes = try std.fmt.allocPrint(c.a(), "0o{}", .{lit_bytes});
5154 lit_bytes = try std.fmtstream.allocPrint(c.a(), "0o{}", .{lit_bytes});
51605155 },
51615156 'X' => {
51625157 // Hexadecimal with capital X, valid in C but not in Zig
5163 lit_bytes = try std.fmt.allocPrint(c.a(), "0x{}", .{lit_bytes[2..]});
5158 lit_bytes = try std.fmtstream.allocPrint(c.a(), "0x{}", .{lit_bytes[2..]});
51645159 },
51655160 else => {},
51665161 }
......@@ -5191,7 +5186,7 @@ fn parseCNumLit(c: *Context, tok: *CToken, source: []const u8, source_loc: ZigCl
51915186 return &cast_node.base;
51925187 } else if (tok.id == .FloatLiteral) {
51935188 if (lit_bytes[0] == '.')
5194 lit_bytes = try std.fmt.allocPrint(c.a(), "0{}", .{lit_bytes});
5189 lit_bytes = try std.fmtstream.allocPrint(c.a(), "0{}", .{lit_bytes});
51955190 if (tok.id.FloatLiteral == .None) {
51965191 return transCreateNodeFloat(c, lit_bytes);
51975192 }
......@@ -5324,7 +5319,7 @@ fn zigifyEscapeSequences(ctx: *Context, source_bytes: []const u8, name: []const
53245319 num += c - 'A' + 10;
53255320 },
53265321 else => {
5327 i += std.fmt.formatIntBuf(bytes[i..], num, 16, false, std.fmt.FormatOptions{ .fill = '0', .width = 2 });
5322 i += std.fmtstream.formatIntBuf(bytes[i..], num, 16, false, std.fmtstream.FormatOptions{ .fill = '0', .width = 2 });
53285323 num = 0;
53295324 if (c == '\\')
53305325 state = .Escape
......@@ -5350,7 +5345,7 @@ fn zigifyEscapeSequences(ctx: *Context, source_bytes: []const u8, name: []const
53505345 };
53515346 num += c - '0';
53525347 } else {
5353 i += std.fmt.formatIntBuf(bytes[i..], num, 16, false, std.fmt.FormatOptions{ .fill = '0', .width = 2 });
5348 i += std.fmtstream.formatIntBuf(bytes[i..], num, 16, false, std.fmtstream.FormatOptions{ .fill = '0', .width = 2 });
53545349 num = 0;
53555350 count = 0;
53565351 if (c == '\\')
......@@ -5364,7 +5359,7 @@ fn zigifyEscapeSequences(ctx: *Context, source_bytes: []const u8, name: []const
53645359 }
53655360 }
53665361 if (state == .Hex or state == .Octal)
5367 i += std.fmt.formatIntBuf(bytes[i..], num, 16, false, std.fmt.FormatOptions{ .fill = '0', .width = 2 });
5362 i += std.fmtstream.formatIntBuf(bytes[i..], num, 16, false, std.fmtstream.FormatOptions{ .fill = '0', .width = 2 });
53685363 return bytes[0..i];
53695364}
53705365