authorgravatar for cody+topolarity@tapscott.meCody Tapscott <cody+topolarity@tapscott.me> 2022-01-24 11:17:52-07:00
committergravatar for cody+topolarity@tapscott.meCody Tapscott <cody+topolarity@tapscott.me> 2022-01-24 12:00:04-07:00
log983dfcd3fbd70347537f7b63db9838848caf0ac0
tree7b47a2eb85d3f76869530ede526d32138c01448b
parent8c96c64fbbab83f14027963d0c4b4da5254a57a8

Fix rendering of `void` function args


2 files changed, 39 insertions(+), 43 deletions(-)

src/codegen/c.zig+37-41
...@@ -633,22 +633,24 @@ pub const DeclGen = struct {...@@ -633,22 +633,24 @@ pub const DeclGen = struct {
633 try dg.renderDeclName(dg.decl, w);633 try dg.renderDeclName(dg.decl, w);
634 try w.writeAll("(");634 try w.writeAll("(");
635 const param_len = dg.decl.ty.fnParamLen();635 const param_len = dg.decl.ty.fnParamLen();
636 const is_var_args = dg.decl.ty.fnIsVarArgs();636
637 if (param_len == 0 and !is_var_args)637 var index: usize = 0;
638 try w.writeAll("void")638 var params_written: usize = 0;
639 else {639 while (index < param_len) : (index += 1) {
640 var index: usize = 0;640 if (dg.decl.ty.fnParamType(index).zigTypeTag() == .Void) continue;
641 while (index < param_len) : (index += 1) {641 if (params_written > 0) {
642 if (index > 0) {642 try w.writeAll(", ");
643 try w.writeAll(", ");
644 }
645 try dg.renderType(w, dg.decl.ty.fnParamType(index));
646 try w.print(" a{d}", .{index});
647 }643 }
644 try dg.renderType(w, dg.decl.ty.fnParamType(index));
645 try w.print(" a{d}", .{index});
646 params_written += 1;
648 }647 }
649 if (is_var_args) {648
650 if (param_len != 0) try w.writeAll(", ");649 if (dg.decl.ty.fnIsVarArgs()) {
650 if (params_written != 0) try w.writeAll(", ");
651 try w.writeAll("...");651 try w.writeAll("...");
652 } else if (params_written == 0) {
653 try w.writeAll("void");
652 }654 }
653 try w.writeByte(')');655 try w.writeByte(')');
654 }656 }
...@@ -670,21 +672,23 @@ pub const DeclGen = struct {...@@ -670,21 +672,23 @@ pub const DeclGen = struct {
670 const name_end = buffer.items.len - 2;672 const name_end = buffer.items.len - 2;
671673
672 const param_len = fn_info.param_types.len;674 const param_len = fn_info.param_types.len;
673 const is_var_args = fn_info.is_var_args;675
674 if (param_len == 0 and !is_var_args)676 var params_written: usize = 0;
675 try bw.writeAll("void")677 var index: usize = 0;
676 else {678 while (index < param_len) : (index += 1) {
677 var index: usize = 0;679 if (fn_info.param_types[index].zigTypeTag() == .Void) continue;
678 while (index < param_len) : (index += 1) {680 if (params_written > 0) {
679 if (index > 0) {681 try bw.writeAll(", ");
680 try bw.writeAll(", ");
681 }
682 try dg.renderType(bw, fn_info.param_types[index]);
683 }682 }
683 try dg.renderType(bw, fn_info.param_types[index]);
684 params_written += 1;
684 }685 }
685 if (is_var_args) {686
686 if (param_len != 0) try bw.writeAll(", ");687 if (fn_info.is_var_args) {
688 if (params_written != 0) try bw.writeAll(", ");
687 try bw.writeAll("...");689 try bw.writeAll("...");
690 } else if (params_written == 0) {
691 try bw.writeAll("void");
688 }692 }
689 try bw.writeAll(");\n");693 try bw.writeAll(");\n");
690694
...@@ -1128,13 +1132,11 @@ pub fn genDecl(o: *Object) !void {...@@ -1128,13 +1132,11 @@ pub fn genDecl(o: *Object) !void {
1128 if (variable.is_threadlocal) {1132 if (variable.is_threadlocal) {
1129 try fwd_decl_writer.writeAll("zig_threadlocal ");1133 try fwd_decl_writer.writeAll("zig_threadlocal ");
1130 }1134 }
1131 try o.dg.renderType(fwd_decl_writer, o.dg.decl.ty);1135
1132 try fwd_decl_writer.writeAll(" ");1136 const decl_c_value: CValue = if (is_global) .{ .bytes = mem.span(o.dg.decl.name) }
1133 if (is_global) {1137 else .{ .decl = o.dg.decl };
1134 try fwd_decl_writer.writeAll(mem.span(o.dg.decl.name));1138
1135 } else {1139 try o.dg.renderTypeAndName(fwd_decl_writer, o.dg.decl.ty, decl_c_value, .Mut, o.dg.decl.align_val);
1136 try o.dg.renderDeclName(o.dg.decl, fwd_decl_writer);
1137 }
1138 try fwd_decl_writer.writeAll(";\n");1140 try fwd_decl_writer.writeAll(";\n");
11391141
1140 if (variable.init.isUndefDeep()) {1142 if (variable.init.isUndefDeep()) {
...@@ -1143,13 +1145,7 @@ pub fn genDecl(o: *Object) !void {...@@ -1143,13 +1145,7 @@ pub fn genDecl(o: *Object) !void {
11431145
1144 try o.indent_writer.insertNewline();1146 try o.indent_writer.insertNewline();
1145 const w = o.writer();1147 const w = o.writer();
1146 try o.dg.renderType(w, o.dg.decl.ty);1148 try o.dg.renderTypeAndName(w, o.dg.decl.ty, decl_c_value, .Mut, o.dg.decl.align_val);
1147 try w.writeAll(" ");
1148 if (is_global) {
1149 try w.writeAll(mem.span(o.dg.decl.name));
1150 } else {
1151 try o.dg.renderDeclName(o.dg.decl, w);
1152 }
1153 try w.writeAll(" = ");1149 try w.writeAll(" = ");
1154 if (variable.init.tag() != .unreachable_value) {1150 if (variable.init.tag() != .unreachable_value) {
1155 try o.dg.renderValue(w, tv.ty, variable.init);1151 try o.dg.renderValue(w, tv.ty, variable.init);
...@@ -2364,9 +2360,9 @@ fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -2364,9 +2360,9 @@ fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue {
2364 try f.writeCValue(writer, local);2360 try f.writeCValue(writer, local);
2365 try writer.writeAll(", &");2361 try writer.writeAll(", &");
2366 try f.writeCValue(writer, operand);2362 try f.writeCValue(writer, operand);
2367 try writer.writeAll(", sizeof ");2363 try writer.writeAll(", sizeof(");
2368 try f.writeCValue(writer, local);2364 try f.writeCValue(writer, local);
2369 try writer.writeAll(");\n");2365 try writer.writeAll("));\n");
23702366
2371 return local;2367 return local;
2372}2368}
test/behavior.zig+2-2
...@@ -67,7 +67,8 @@ test {...@@ -67,7 +67,8 @@ test {
67 // Tests that pass for stage1, llvm backend, C backend67 // Tests that pass for stage1, llvm backend, C backend
68 _ = @import("behavior/cast_int.zig");68 _ = @import("behavior/cast_int.zig");
69 _ = @import("behavior/int128.zig");69 _ = @import("behavior/int128.zig");
70 _ = @import("behavior/translate_c_macros.zig");70 _ = @import("behavior/union.zig");
71// _ = @import("behavior/translate_c_macros.zig");
7172
72 if (builtin.zig_backend != .stage2_c) {73 if (builtin.zig_backend != .stage2_c) {
73 // Tests that pass for stage1 and the llvm backend.74 // Tests that pass for stage1 and the llvm backend.
...@@ -110,7 +111,6 @@ test {...@@ -110,7 +111,6 @@ test {
110 _ = @import("behavior/slice.zig");111 _ = @import("behavior/slice.zig");
111 _ = @import("behavior/struct_llvm.zig");112 _ = @import("behavior/struct_llvm.zig");
112 _ = @import("behavior/switch.zig");113 _ = @import("behavior/switch.zig");
113 _ = @import("behavior/union.zig");
114 _ = @import("behavior/widening.zig");114 _ = @import("behavior/widening.zig");
115115
116 if (builtin.zig_backend != .stage1) {116 if (builtin.zig_backend != .stage1) {