@@ -172,7 +172,7 @@ pub const Options = struct {
172172 module_libs : bool ,
173173};
174174
175- pub fn translate (options : Options ) ! []u8 {
175+ pub fn translate (options : Options ) mem.Allocator.Error ! []u8 {
176176 const gpa = options .gpa ;
177177 var arena_allocator = std .heap .ArenaAllocator .init (gpa );
178178 defer arena_allocator .deinit ();
@@ -217,32 +217,32 @@ pub fn translate(options: Options) ![]u8 {
217217
218218 try translator .global_scope .processContainerMemberFns ();
219219
220- var buf : std .ArrayList ( u8 ) = .init (gpa );
221- defer buf .deinit ();
220+ var allocating : std.Io.Writer.Allocating = .init (gpa );
221+ defer allocating .deinit ();
222222
223223 if (options .module_libs ) {
224- try buf . appendSlice (
224+ allocating . writer . writeAll (
225225 \\pub const __builtin = @import("c_builtins");
226226 \\pub const __helpers = @import("helpers");
227227 \\
228228 \\
229- );
229+ ) catch return error . OutOfMemory ;
230230 } else {
231- try buf . appendSlice (
231+ allocating . writer . writeAll (
232232 \\pub const __builtin = @import("c_builtins.zig");
233233 \\pub const __helpers = @import("helpers.zig");
234234 \\
235235 \\
236- );
236+ ) catch return error . OutOfMemory ;
237237 }
238238
239239 var zig_ast = try ast .render (gpa , translator .global_scope .nodes .items );
240240 defer {
241241 gpa .free (zig_ast .source );
242242 zig_ast .deinit (gpa );
243243 }
244- try zig_ast .renderToArrayList ( & buf , .{});
245- return buf .toOwnedSlice ();
244+ zig_ast .render ( gpa , & allocating . writer , .{}) catch return error . OutOfMemory ;
245+ return allocating .toOwnedSlice ();
246246}
247247
248248fn prepopulateGlobalNameTable (t : * Translator ) ! void {
@@ -1006,17 +1006,16 @@ fn transStaticAssert(t: *Translator, scope: *Scope, static_assert: Node.StaticAs
10061006 const str_qt = message .qt (t .tree );
10071007
10081008 const bytes = t .comp .interner .get (str_val .ref ()).bytes ;
1009- var buf = std .ArrayList ( u8 ) .init (t .gpa );
1010- defer buf .deinit ();
1009+ var allocating : std.Io.Writer.Allocating = .init (t .gpa );
1010+ defer allocating .deinit ();
10111011
1012- try buf . appendSlice ("\" static assertion failed \\ " );
1012+ allocating . writer . writeAll ("\" static assertion failed \\ " ) catch return error . OutOfMemory ;
10131013
1014- try buf .ensureUnusedCapacity (bytes .len );
1015- try aro .Value .printString (bytes , str_qt , t .comp , buf .writer ());
1016- _ = buf .pop (); // printString adds a terminating " so we need to remove it
1017- try buf .appendSlice ("\\ \"\" " );
1014+ aro .Value .printString (bytes , str_qt , t .comp , & allocating .writer ) catch return error .OutOfMemory ;
1015+ allocating .writer .end -= 1 ; // printString adds a terminating " so we need to remove it
1016+ allocating .writer .writeAll ("\\ \"\" " ) catch return error .OutOfMemory ;
10181017
1019- break :str try ZigTag .string_literal .create (t .arena , try t .arena .dupe (u8 , buf . items ));
1018+ break :str try ZigTag .string_literal .create (t .arena , try t .arena .dupe (u8 , allocating . getWritten () ));
10201019 } else try ZigTag .string_literal .create (t .arena , "\" static assertion failed\" " );
10211020
10221021 const assert_node = try ZigTag .static_assert .create (t .arena , .{ .lhs = condition , .rhs = diagnostic });
@@ -1027,11 +1026,11 @@ fn transGlobalAsm(t: *Translator, scope: *Scope, global_asm: Node.SimpleAsm) Err
10271026 const asm_string = t .tree .value_map .get (global_asm .asm_str ).? ;
10281027 const bytes = t .comp .interner .get (asm_string .ref ()).bytes ;
10291028
1030- var buf = std . ArrayList ( u8 ). init ( t .gpa );
1031- defer buf .deinit ();
1032- try aro .Value .printString (bytes , global_asm .asm_str .qt (t .tree ), t .comp , buf .writer ()) ;
1029+ var allocating : std.Io.Writer.Allocating = try . initCapacity ( t .gpa , bytes . len );
1030+ defer allocating .deinit ();
1031+ aro .Value .printString (bytes , global_asm .asm_str .qt (t .tree ), t .comp , & allocating .writer ) catch return error . OutOfMemory ;
10331032
1034- const str_node = try ZigTag .string_literal .create (t .arena , try t .arena .dupe (u8 , buf . items ));
1033+ const str_node = try ZigTag .string_literal .create (t .arena , try t .arena .dupe (u8 , allocating . getWritten () ));
10351034
10361035 const asm_node = try ZigTag .asm_simple .create (t .arena , str_node );
10371036 const block = try ZigTag .block_single .create (t .arena , asm_node );
@@ -1045,11 +1044,10 @@ fn transGlobalAsm(t: *Translator, scope: *Scope, global_asm: Node.SimpleAsm) Err
10451044// ================
10461045
10471046fn getTypeStr (t : * Translator , qt : QualType ) ! []const u8 {
1048- var buf : std .ArrayListUnmanaged (u8 ) = .empty ;
1049- defer buf .deinit (t .gpa );
1050- const w = buf .writer (t .gpa );
1051- try qt .print (t .comp , w );
1052- return t .arena .dupe (u8 , buf .items );
1047+ var allocating : std.Io.Writer.Allocating = .init (t .gpa );
1048+ defer allocating .deinit ();
1049+ qt .print (t .comp , & allocating .writer ) catch return error .OutOfMemory ;
1050+ return t .arena .dupe (u8 , allocating .getWritten ());
10531051}
10541052
10551053fn transType (t : * Translator , scope : * Scope , qt : QualType , source_loc : TokenIndex ) TypeError ! ZigNode {
@@ -3353,12 +3351,11 @@ fn transFloatLiteral(
33533351 const val = t .tree .value_map .get (literal_index ).? ;
33543352 const float_literal = literal_index .get (t .tree ).float_literal ;
33553353
3356- var buf : std .ArrayListUnmanaged (u8 ) = .empty ;
3357- defer buf .deinit (t .gpa );
3358- const w = buf .writer (t .gpa );
3359- _ = try val .print (float_literal .qt , t .comp , w );
3354+ var allocating : std.Io.Writer.Allocating = .init (t .gpa );
3355+ defer allocating .deinit ();
3356+ _ = val .print (float_literal .qt , t .comp , & allocating .writer ) catch return error .OutOfMemory ;
33603357
3361- const float_lit_node = try ZigTag .float_literal .create (t .arena , try t .arena .dupe (u8 , buf . items ));
3358+ const float_lit_node = try ZigTag .float_literal .create (t .arena , try t .arena .dupe (u8 , allocating . getWritten () ));
33623359 if (suppress_as == .no_as ) {
33633360 return t .maybeSuppressResult (used , float_lit_node );
33643361 }
@@ -3398,13 +3395,12 @@ fn transNarrowStringLiteral(
33983395 const val = t .tree .value_map .get (expr ).? ;
33993396
34003397 const bytes = t .comp .interner .get (val .ref ()).bytes ;
3401- var buf = std . ArrayList ( u8 ). init ( t .gpa );
3402- defer buf .deinit ();
3398+ var allocating : std.Io.Writer.Allocating = try . initCapacity ( t .gpa , bytes . len );
3399+ defer allocating .deinit ();
34033400
3404- try buf .ensureUnusedCapacity (bytes .len );
3405- try aro .Value .printString (bytes , literal .qt , t .comp , buf .writer ());
3401+ aro .Value .printString (bytes , literal .qt , t .comp , & allocating .writer ) catch return error .OutOfMemory ;
34063402
3407- return ZigTag .string_literal .create (t .arena , try t .arena .dupe (u8 , buf . items ));
3403+ return ZigTag .string_literal .create (t .arena , try t .arena .dupe (u8 , allocating . getWritten () ));
34083404}
34093405
34103406/// Translate a string literal that is initializing an array. In general narrow string
@@ -3887,7 +3883,7 @@ fn createNumberNode(t: *Translator, num: anytype, num_kind: enum { int, float })
38873883
38883884fn createCharLiteralNode (t : * Translator , narrow : bool , val : u32 ) TransError ! ZigNode {
38893885 return ZigTag .char_literal .create (t .arena , if (narrow )
3890- try std .fmt .allocPrint (t .arena , "'{' }'" , .{std .zig .fmtEscapes (&.{@as (u8 , @intCast (val ))})})
3886+ try std .fmt .allocPrint (t .arena , "'{f }'" , .{std .zig .fmtChar (&.{@as (u8 , @intCast (val ))})})
38913887 else
38923888 try std .fmt .allocPrint (t .arena , "'\\ u{{{x}}}'" , .{val }));
38933889}
0 commit comments