diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-10-27 22:09:17 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-27 22:09:17 -0400 |
| commit | 2991e4a454c4d719226fcfc7e8ac8dec44250a7e (patch) | |
| tree | 2d0abcbd613008f7c07aae6bcad1b656bffb1f4f /src/codegen/c.zig | |
| parent | bc72ae5e4e6d8f2253aed1316b053ad1022f9f67 (diff) | |
| parent | 648d34d8eacaf2e35e336abd5b0c50c2ab9bfc94 (diff) | |
| download | zig-2991e4a454c4d719226fcfc7e8ac8dec44250a7e.tar.gz zig-2991e4a454c4d719226fcfc7e8ac8dec44250a7e.zip | |
Merge pull request #13288 from Vexu/opt-slice
Optimize size of optional slices (+ some fixes)
Diffstat (limited to 'src/codegen/c.zig')
| -rw-r--r-- | src/codegen/c.zig | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig index d0f76f0390..6ae11c500d 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -730,7 +730,11 @@ pub const DeclGen = struct { } if (ty.optionalReprIsPayload()) { - return dg.renderValue(writer, payload_ty, val, location); + if (val.castTag(.opt_payload)) |payload| { + return dg.renderValue(writer, payload_ty, payload.data, location); + } else { + return dg.renderValue(writer, payload_ty, val, location); + } } try writer.writeByte('('); @@ -3267,11 +3271,9 @@ fn airIsNull( try f.writeCValue(writer, operand); const ty = f.air.typeOf(un_op); + const opt_ty = if (deref_suffix[0] != 0) ty.childType() else ty; var opt_buf: Type.Payload.ElemType = undefined; - const payload_ty = if (deref_suffix[0] != 0) - ty.childType().optionalChild(&opt_buf) - else - ty.optionalChild(&opt_buf); + const payload_ty = opt_ty.optionalChild(&opt_buf); if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { try writer.print("){s} {s} true;\n", .{ deref_suffix, operator }); @@ -3280,6 +3282,8 @@ fn airIsNull( try writer.print("){s} {s} NULL;\n", .{ deref_suffix, operator }); } else if (payload_ty.zigTypeTag() == .ErrorSet) { try writer.print("){s} {s} 0;\n", .{ deref_suffix, operator }); + } else if (payload_ty.isSlice() and opt_ty.optionalReprIsPayload()) { + try writer.print("){s}.ptr {s} NULL;\n", .{ deref_suffix, operator }); } else { try writer.print("){s}.is_null {s} true;\n", .{ deref_suffix, operator }); } |
