| ... | ... | @@ -1067,6 +1067,8 @@ pub const DeclGen = struct { |
| 1067 | 1067 | } |
| 1068 | 1068 | |
| 1069 | 1069 | fn renderSliceTypedef(dg: *DeclGen, t: Type) error{ OutOfMemory, AnalysisFail }![]const u8 { |
| 1070 | std.debug.assert(t.sentinel() == null); // expected canonical type |
| 1071 | |
| 1070 | 1072 | var buffer = std.ArrayList(u8).init(dg.typedefs.allocator); |
| 1071 | 1073 | defer buffer.deinit(); |
| 1072 | 1074 | const bw = buffer.writer(); |
| ... | ... | @@ -1078,22 +1080,12 @@ pub const DeclGen = struct { |
| 1078 | 1080 | const ptr_name = CValue{ .bytes = "ptr" }; |
| 1079 | 1081 | try dg.renderTypeAndName(bw, ptr_type, ptr_name, .Mut, 0); |
| 1080 | 1082 | |
| 1081 | | const ptr_sentinel = ptr_type.ptrInfo().data.sentinel; |
| 1082 | | const child_type = t.childType(); |
| 1083 | | |
| 1084 | 1083 | try bw.writeAll("; size_t len; } "); |
| 1085 | 1084 | const name_begin = buffer.items.len; |
| 1086 | 1085 | try bw.print("zig_{c}_{}", .{ |
| 1087 | 1086 | @as(u8, if (t.isConstPtr()) 'L' else 'M'), |
| 1088 | | typeToCIdentifier(child_type, dg.module), |
| 1087 | typeToCIdentifier(t.childType(), dg.module), |
| 1089 | 1088 | }); |
| 1090 | | if (ptr_sentinel) |s| { |
| 1091 | | var sentinel_buffer = std.ArrayList(u8).init(dg.typedefs.allocator); |
| 1092 | | defer sentinel_buffer.deinit(); |
| 1093 | | |
| 1094 | | try dg.renderValue(sentinel_buffer.writer(), child_type, s, .Identifier); |
| 1095 | | try bw.print("_s_{}", .{fmtIdent(sentinel_buffer.items)}); |
| 1096 | | } |
| 1097 | 1089 | const name_end = buffer.items.len; |
| 1098 | 1090 | try bw.writeAll(";\n"); |
| 1099 | 1091 | |
| ... | ... | @@ -1351,28 +1343,21 @@ pub const DeclGen = struct { |
| 1351 | 1343 | } |
| 1352 | 1344 | |
| 1353 | 1345 | fn renderArrayTypedef(dg: *DeclGen, t: Type) error{ OutOfMemory, AnalysisFail }![]const u8 { |
| 1346 | const info = t.arrayInfo(); |
| 1347 | std.debug.assert(info.sentinel == null); // expected canonical type |
| 1348 | |
| 1354 | 1349 | var buffer = std.ArrayList(u8).init(dg.typedefs.allocator); |
| 1355 | 1350 | defer buffer.deinit(); |
| 1356 | 1351 | const bw = buffer.writer(); |
| 1357 | 1352 | |
| 1358 | | const elem_type = t.elemType(); |
| 1359 | | |
| 1360 | 1353 | try bw.writeAll("typedef "); |
| 1361 | | try dg.renderType(bw, elem_type); |
| 1354 | try dg.renderType(bw, info.elem_type); |
| 1362 | 1355 | |
| 1363 | 1356 | const name_begin = buffer.items.len + " ".len; |
| 1364 | | try bw.print(" zig_A_{}_{d}", .{ typeToCIdentifier(elem_type, dg.module), t.arrayLen() }); |
| 1365 | | if (t.sentinel()) |s| { |
| 1366 | | var sentinel_buffer = std.ArrayList(u8).init(dg.typedefs.allocator); |
| 1367 | | defer sentinel_buffer.deinit(); |
| 1368 | | |
| 1369 | | try dg.renderValue(sentinel_buffer.writer(), elem_type, s, .Identifier); |
| 1370 | | try bw.print("_s_{}", .{fmtIdent(sentinel_buffer.items)}); |
| 1371 | | } |
| 1357 | try bw.print(" zig_A_{}_{d}", .{ typeToCIdentifier(info.elem_type, dg.module), info.len }); |
| 1372 | 1358 | const name_end = buffer.items.len; |
| 1373 | 1359 | |
| 1374 | | const c_len = t.arrayLenIncludingSentinel(); |
| 1375 | | try bw.print("[{d}];\n", .{if (c_len > 0) c_len else 1}); |
| 1360 | try bw.print("[{d}];\n", .{if (info.len > 0) info.len else 1}); |
| 1376 | 1361 | |
| 1377 | 1362 | const rendered = buffer.toOwnedSlice(); |
| 1378 | 1363 | errdefer dg.typedefs.allocator.free(rendered); |
| ... | ... | @@ -1509,8 +1494,14 @@ pub const DeclGen = struct { |
| 1509 | 1494 | }, |
| 1510 | 1495 | .Pointer => { |
| 1511 | 1496 | if (t.isSlice()) { |
| 1512 | | const name = dg.getTypedefName(t) orelse |
| 1513 | | try dg.renderSliceTypedef(t); |
| 1497 | var slice_ty_pl = Type.Payload.ElemType{ |
| 1498 | .base = .{ .tag = if (t.ptrIsMutable()) .mut_slice else .const_slice }, |
| 1499 | .data = t.childType(), |
| 1500 | }; |
| 1501 | const slice_ty = Type.initPayload(&slice_ty_pl.base); |
| 1502 | |
| 1503 | const name = dg.getTypedefName(slice_ty) orelse |
| 1504 | try dg.renderSliceTypedef(slice_ty); |
| 1514 | 1505 | |
| 1515 | 1506 | return w.writeAll(name); |
| 1516 | 1507 | } |
| ... | ... | @@ -1541,8 +1532,14 @@ pub const DeclGen = struct { |
| 1541 | 1532 | return w.writeAll(" *"); |
| 1542 | 1533 | }, |
| 1543 | 1534 | .Array => { |
| 1544 | | const name = dg.getTypedefName(t) orelse |
| 1545 | | try dg.renderArrayTypedef(t); |
| 1535 | var array_ty_pl = Type.Payload.Array{ .base = .{ .tag = .array }, .data = .{ |
| 1536 | .len = t.arrayLenIncludingSentinel(), |
| 1537 | .elem_type = t.childType(), |
| 1538 | } }; |
| 1539 | const array_ty = Type.initPayload(&array_ty_pl.base); |
| 1540 | |
| 1541 | const name = dg.getTypedefName(array_ty) orelse |
| 1542 | try dg.renderArrayTypedef(array_ty); |
| 1546 | 1543 | |
| 1547 | 1544 | return w.writeAll(name); |
| 1548 | 1545 | }, |