| ... | ... | @@ -522,7 +522,7 @@ pub const Object = struct { |
| 522 | 522 | pub const DeclGen = struct { |
| 523 | 523 | gpa: mem.Allocator, |
| 524 | 524 | module: *Module, |
| 525 | | decl_index: Decl.OptionalIndex, |
| 525 | pass: Pass, |
| 526 | 526 | is_naked_fn: bool, |
| 527 | 527 | /// This is a borrowed reference from `link.C`. |
| 528 | 528 | fwd_decl: std.ArrayList(u8), |
| ... | ... | @@ -533,10 +533,16 @@ pub const DeclGen = struct { |
| 533 | 533 | anon_decl_deps: std.AutoArrayHashMapUnmanaged(InternPool.Index, C.DeclBlock), |
| 534 | 534 | aligned_anon_decls: std.AutoArrayHashMapUnmanaged(InternPool.Index, Alignment), |
| 535 | 535 | |
| 536 | pub const Pass = union(enum) { |
| 537 | decl: Decl.Index, |
| 538 | anon: InternPool.Index, |
| 539 | flush, |
| 540 | }; |
| 541 | |
| 536 | 542 | fn fail(dg: *DeclGen, comptime format: []const u8, args: anytype) error{ AnalysisFail, OutOfMemory } { |
| 537 | 543 | @setCold(true); |
| 538 | 544 | const mod = dg.module; |
| 539 | | const decl_index = dg.decl_index.unwrap().?; |
| 545 | const decl_index = dg.pass.decl; |
| 540 | 546 | const decl = mod.declPtr(decl_index); |
| 541 | 547 | const src = LazySrcLoc.nodeOffset(0); |
| 542 | 548 | const src_loc = src.toSrcLoc(decl, mod); |
| ... | ... | @@ -1566,18 +1572,11 @@ pub const DeclGen = struct { |
| 1566 | 1572 | else => unreachable, |
| 1567 | 1573 | } |
| 1568 | 1574 | } |
| 1569 | | if (fn_decl.val.getFunction(mod)) |func| if (func.analysis(ip).is_cold) try w.writeAll("zig_cold "); |
| 1575 | if (fn_decl.val.getFunction(mod)) |func| if (func.analysis(ip).is_cold) |
| 1576 | try w.writeAll("zig_cold "); |
| 1570 | 1577 | if (fn_info.return_type == .noreturn_type) try w.writeAll("zig_noreturn "); |
| 1571 | 1578 | |
| 1572 | | const trailing = try renderTypePrefix( |
| 1573 | | dg.decl_index, |
| 1574 | | store.*, |
| 1575 | | mod, |
| 1576 | | w, |
| 1577 | | fn_cty_idx, |
| 1578 | | .suffix, |
| 1579 | | .{}, |
| 1580 | | ); |
| 1579 | const trailing = try renderTypePrefix(dg.pass, store.*, mod, w, fn_cty_idx, .suffix, .{}); |
| 1581 | 1580 | try w.print("{}", .{trailing}); |
| 1582 | 1581 | |
| 1583 | 1582 | if (toCallingConvention(fn_info.cc)) |call_conv| { |
| ... | ... | @@ -1597,7 +1596,7 @@ pub const DeclGen = struct { |
| 1597 | 1596 | } |
| 1598 | 1597 | |
| 1599 | 1598 | try renderTypeSuffix( |
| 1600 | | dg.decl_index, |
| 1599 | dg.pass, |
| 1601 | 1600 | store.*, |
| 1602 | 1601 | mod, |
| 1603 | 1602 | w, |
| ... | ... | @@ -1652,8 +1651,8 @@ pub const DeclGen = struct { |
| 1652 | 1651 | fn renderCType(dg: *DeclGen, w: anytype, idx: CType.Index) error{ OutOfMemory, AnalysisFail }!void { |
| 1653 | 1652 | const store = &dg.ctypes.set; |
| 1654 | 1653 | const mod = dg.module; |
| 1655 | | _ = try renderTypePrefix(dg.decl_index, store.*, mod, w, idx, .suffix, .{}); |
| 1656 | | try renderTypeSuffix(dg.decl_index, store.*, mod, w, idx, .suffix, .{}); |
| 1654 | _ = try renderTypePrefix(dg.pass, store.*, mod, w, idx, .suffix, .{}); |
| 1655 | try renderTypeSuffix(dg.pass, store.*, mod, w, idx, .suffix, .{}); |
| 1657 | 1656 | } |
| 1658 | 1657 | |
| 1659 | 1658 | const IntCastContext = union(enum) { |
| ... | ... | @@ -1799,11 +1798,10 @@ pub const DeclGen = struct { |
| 1799 | 1798 | .gt => try w.print("zig_align({}) ", .{alignas.toByteUnits()}), |
| 1800 | 1799 | } |
| 1801 | 1800 | |
| 1802 | | const trailing = |
| 1803 | | try renderTypePrefix(dg.decl_index, store.*, mod, w, cty_idx, .suffix, qualifiers); |
| 1801 | const trailing = try renderTypePrefix(dg.pass, store.*, mod, w, cty_idx, .suffix, qualifiers); |
| 1804 | 1802 | try w.print("{}", .{trailing}); |
| 1805 | 1803 | try dg.writeCValue(w, name); |
| 1806 | | try renderTypeSuffix(dg.decl_index, store.*, mod, w, cty_idx, .suffix, .{}); |
| 1804 | try renderTypeSuffix(dg.pass, store.*, mod, w, cty_idx, .suffix, .{}); |
| 1807 | 1805 | } |
| 1808 | 1806 | |
| 1809 | 1807 | fn declIsGlobal(dg: *DeclGen, tv: TypedValue) bool { |
| ... | ... | @@ -2070,7 +2068,7 @@ fn renderTypeName( |
| 2070 | 2068 | } |
| 2071 | 2069 | } |
| 2072 | 2070 | fn renderTypePrefix( |
| 2073 | | decl: Decl.OptionalIndex, |
| 2071 | pass: DeclGen.Pass, |
| 2074 | 2072 | store: CType.Store.Set, |
| 2075 | 2073 | mod: *Module, |
| 2076 | 2074 | w: anytype, |
| ... | ... | @@ -2128,7 +2126,7 @@ fn renderTypePrefix( |
| 2128 | 2126 | => |tag| { |
| 2129 | 2127 | const child_idx = cty.cast(CType.Payload.Child).?.data; |
| 2130 | 2128 | const child_trailing = try renderTypePrefix( |
| 2131 | | decl, |
| 2129 | pass, |
| 2132 | 2130 | store, |
| 2133 | 2131 | mod, |
| 2134 | 2132 | w, |
| ... | ... | @@ -2152,15 +2150,8 @@ fn renderTypePrefix( |
| 2152 | 2150 | .vector, |
| 2153 | 2151 | => { |
| 2154 | 2152 | const child_idx = cty.cast(CType.Payload.Sequence).?.data.elem_type; |
| 2155 | | const child_trailing = try renderTypePrefix( |
| 2156 | | decl, |
| 2157 | | store, |
| 2158 | | mod, |
| 2159 | | w, |
| 2160 | | child_idx, |
| 2161 | | .suffix, |
| 2162 | | qualifiers, |
| 2163 | | ); |
| 2153 | const child_trailing = |
| 2154 | try renderTypePrefix(pass, store, mod, w, child_idx, .suffix, qualifiers); |
| 2164 | 2155 | switch (parent_fix) { |
| 2165 | 2156 | .prefix => { |
| 2166 | 2157 | try w.print("{}(", .{child_trailing}); |
| ... | ... | @@ -2172,10 +2163,11 @@ fn renderTypePrefix( |
| 2172 | 2163 | |
| 2173 | 2164 | .fwd_anon_struct, |
| 2174 | 2165 | .fwd_anon_union, |
| 2175 | | => if (decl.unwrap()) |decl_index| |
| 2176 | | try w.print("anon__{d}_{d}", .{ @intFromEnum(decl_index), idx }) |
| 2177 | | else |
| 2178 | | try renderTypeName(mod, w, idx, cty, ""), |
| 2166 | => switch (pass) { |
| 2167 | .decl => |decl_index| try w.print("decl__{d}_{d}", .{ @intFromEnum(decl_index), idx }), |
| 2168 | .anon => |anon_decl| try w.print("anon__{d}_{d}", .{ @intFromEnum(anon_decl), idx }), |
| 2169 | .flush => try renderTypeName(mod, w, idx, cty, ""), |
| 2170 | }, |
| 2179 | 2171 | |
| 2180 | 2172 | .fwd_struct, |
| 2181 | 2173 | .fwd_union, |
| ... | ... | @@ -2201,7 +2193,7 @@ fn renderTypePrefix( |
| 2201 | 2193 | .packed_struct, |
| 2202 | 2194 | .packed_union, |
| 2203 | 2195 | => return renderTypePrefix( |
| 2204 | | decl, |
| 2196 | pass, |
| 2205 | 2197 | store, |
| 2206 | 2198 | mod, |
| 2207 | 2199 | w, |
| ... | ... | @@ -2214,7 +2206,7 @@ fn renderTypePrefix( |
| 2214 | 2206 | .varargs_function, |
| 2215 | 2207 | => { |
| 2216 | 2208 | const child_trailing = try renderTypePrefix( |
| 2217 | | decl, |
| 2209 | pass, |
| 2218 | 2210 | store, |
| 2219 | 2211 | mod, |
| 2220 | 2212 | w, |
| ... | ... | @@ -2241,7 +2233,7 @@ fn renderTypePrefix( |
| 2241 | 2233 | return trailing; |
| 2242 | 2234 | } |
| 2243 | 2235 | fn renderTypeSuffix( |
| 2244 | | decl: Decl.OptionalIndex, |
| 2236 | pass: DeclGen.Pass, |
| 2245 | 2237 | store: CType.Store.Set, |
| 2246 | 2238 | mod: *Module, |
| 2247 | 2239 | w: anytype, |
| ... | ... | @@ -2295,7 +2287,7 @@ fn renderTypeSuffix( |
| 2295 | 2287 | .pointer_volatile, |
| 2296 | 2288 | .pointer_const_volatile, |
| 2297 | 2289 | => try renderTypeSuffix( |
| 2298 | | decl, |
| 2290 | pass, |
| 2299 | 2291 | store, |
| 2300 | 2292 | mod, |
| 2301 | 2293 | w, |
| ... | ... | @@ -2314,7 +2306,7 @@ fn renderTypeSuffix( |
| 2314 | 2306 | |
| 2315 | 2307 | try w.print("[{}]", .{cty.cast(CType.Payload.Sequence).?.data.len}); |
| 2316 | 2308 | try renderTypeSuffix( |
| 2317 | | decl, |
| 2309 | pass, |
| 2318 | 2310 | store, |
| 2319 | 2311 | mod, |
| 2320 | 2312 | w, |
| ... | ... | @@ -2356,9 +2348,9 @@ fn renderTypeSuffix( |
| 2356 | 2348 | if (need_comma) try w.writeAll(", "); |
| 2357 | 2349 | need_comma = true; |
| 2358 | 2350 | const trailing = |
| 2359 | | try renderTypePrefix(decl, store, mod, w, param_type, .suffix, qualifiers); |
| 2351 | try renderTypePrefix(pass, store, mod, w, param_type, .suffix, qualifiers); |
| 2360 | 2352 | if (qualifiers.contains(.@"const")) try w.print("{}a{d}", .{ trailing, param_i }); |
| 2361 | | try renderTypeSuffix(decl, store, mod, w, param_type, .suffix, .{}); |
| 2353 | try renderTypeSuffix(pass, store, mod, w, param_type, .suffix, .{}); |
| 2362 | 2354 | } |
| 2363 | 2355 | switch (tag) { |
| 2364 | 2356 | .function => {}, |
| ... | ... | @@ -2372,7 +2364,7 @@ fn renderTypeSuffix( |
| 2372 | 2364 | if (!need_comma) try w.writeAll("void"); |
| 2373 | 2365 | try w.writeByte(')'); |
| 2374 | 2366 | |
| 2375 | | try renderTypeSuffix(decl, store, mod, w, data.return_type, .suffix, .{}); |
| 2367 | try renderTypeSuffix(pass, store, mod, w, data.return_type, .suffix, .{}); |
| 2376 | 2368 | }, |
| 2377 | 2369 | } |
| 2378 | 2370 | } |
| ... | ... | @@ -2392,9 +2384,9 @@ fn renderAggregateFields( |
| 2392 | 2384 | .eq => {}, |
| 2393 | 2385 | .gt => try writer.print("zig_align({}) ", .{field.alignas.toByteUnits()}), |
| 2394 | 2386 | } |
| 2395 | | const trailing = try renderTypePrefix(.none, store, mod, writer, field.type, .suffix, .{}); |
| 2387 | const trailing = try renderTypePrefix(.flush, store, mod, writer, field.type, .suffix, .{}); |
| 2396 | 2388 | try writer.print("{}{ }", .{ trailing, fmtIdent(mem.span(field.name)) }); |
| 2397 | | try renderTypeSuffix(.none, store, mod, writer, field.type, .suffix, .{}); |
| 2389 | try renderTypeSuffix(.flush, store, mod, writer, field.type, .suffix, .{}); |
| 2398 | 2390 | try writer.writeAll(";\n"); |
| 2399 | 2391 | } |
| 2400 | 2392 | try writer.writeByteNTimes(' ', indent); |
| ... | ... | @@ -2406,18 +2398,18 @@ pub fn genTypeDecl( |
| 2406 | 2398 | writer: anytype, |
| 2407 | 2399 | global_store: CType.Store.Set, |
| 2408 | 2400 | global_idx: CType.Index, |
| 2409 | | decl: Decl.OptionalIndex, |
| 2401 | pass: DeclGen.Pass, |
| 2410 | 2402 | decl_store: CType.Store.Set, |
| 2411 | 2403 | decl_idx: CType.Index, |
| 2412 | 2404 | found_existing: bool, |
| 2413 | 2405 | ) !void { |
| 2414 | 2406 | const global_cty = global_store.indexToCType(global_idx); |
| 2415 | 2407 | switch (global_cty.tag()) { |
| 2416 | | .fwd_anon_struct => if (decl != .none) { |
| 2408 | .fwd_anon_struct => if (pass != .flush) { |
| 2417 | 2409 | try writer.writeAll("typedef "); |
| 2418 | | _ = try renderTypePrefix(.none, global_store, mod, writer, global_idx, .suffix, .{}); |
| 2410 | _ = try renderTypePrefix(.flush, global_store, mod, writer, global_idx, .suffix, .{}); |
| 2419 | 2411 | try writer.writeByte(' '); |
| 2420 | | _ = try renderTypePrefix(decl, decl_store, mod, writer, decl_idx, .suffix, .{}); |
| 2412 | _ = try renderTypePrefix(pass, decl_store, mod, writer, decl_idx, .suffix, .{}); |
| 2421 | 2413 | try writer.writeAll(";\n"); |
| 2422 | 2414 | }, |
| 2423 | 2415 | |
| ... | ... | @@ -2435,7 +2427,15 @@ pub fn genTypeDecl( |
| 2435 | 2427 | .fwd_union, |
| 2436 | 2428 | => { |
| 2437 | 2429 | const owner_decl = global_cty.cast(CType.Payload.FwdDecl).?.data; |
| 2438 | | _ = try renderTypePrefix(.none, global_store, mod, writer, global_idx, .suffix, .{}); |
| 2430 | _ = try renderTypePrefix( |
| 2431 | .flush, |
| 2432 | global_store, |
| 2433 | mod, |
| 2434 | writer, |
| 2435 | global_idx, |
| 2436 | .suffix, |
| 2437 | .{}, |
| 2438 | ); |
| 2439 | 2439 | try writer.writeAll("; // "); |
| 2440 | 2440 | try mod.declPtr(owner_decl).renderFullyQualifiedName(mod, writer); |
| 2441 | 2441 | try writer.writeByte('\n'); |
| ... | ... | @@ -2552,7 +2552,7 @@ fn genExports(o: *Object) !void { |
| 2552 | 2552 | |
| 2553 | 2553 | const mod = o.dg.module; |
| 2554 | 2554 | const ip = &mod.intern_pool; |
| 2555 | | const decl_index = o.dg.decl_index.unwrap().?; |
| 2555 | const decl_index = o.dg.pass.decl; |
| 2556 | 2556 | const decl = mod.declPtr(decl_index); |
| 2557 | 2557 | const tv: TypedValue = .{ .ty = decl.ty, .val = (try decl.internValue(mod)).toValue() }; |
| 2558 | 2558 | const fwd = o.dg.fwd_decl.writer(); |
| ... | ... | @@ -2692,7 +2692,7 @@ pub fn genFunc(f: *Function) !void { |
| 2692 | 2692 | const o = &f.object; |
| 2693 | 2693 | const mod = o.dg.module; |
| 2694 | 2694 | const gpa = o.dg.gpa; |
| 2695 | | const decl_index = o.dg.decl_index.unwrap().?; |
| 2695 | const decl_index = o.dg.pass.decl; |
| 2696 | 2696 | const decl = mod.declPtr(decl_index); |
| 2697 | 2697 | const tv: TypedValue = .{ |
| 2698 | 2698 | .ty = decl.ty, |
| ... | ... | @@ -2779,7 +2779,7 @@ pub fn genDecl(o: *Object) !void { |
| 2779 | 2779 | defer tracy.end(); |
| 2780 | 2780 | |
| 2781 | 2781 | const mod = o.dg.module; |
| 2782 | | const decl_index = o.dg.decl_index.unwrap().?; |
| 2782 | const decl_index = o.dg.pass.decl; |
| 2783 | 2783 | const decl = mod.declPtr(decl_index); |
| 2784 | 2784 | const tv: TypedValue = .{ .ty = decl.ty, .val = (try decl.internValue(mod)).toValue() }; |
| 2785 | 2785 | |
| ... | ... | @@ -2825,13 +2825,13 @@ pub fn genDeclValue( |
| 2825 | 2825 | alignment: Alignment, |
| 2826 | 2826 | link_section: InternPool.OptionalNullTerminatedString, |
| 2827 | 2827 | ) !void { |
| 2828 | const mod = o.dg.module; |
| 2828 | 2829 | const fwd_decl_writer = o.dg.fwd_decl.writer(); |
| 2829 | 2830 | |
| 2830 | 2831 | try fwd_decl_writer.writeAll(if (is_global) "zig_extern " else "static "); |
| 2831 | 2832 | try o.dg.renderTypeAndName(fwd_decl_writer, tv.ty, decl_c_value, Const, alignment, .complete); |
| 2832 | 2833 | try fwd_decl_writer.writeAll(";\n"); |
| 2833 | 2834 | |
| 2834 | | const mod = o.dg.module; |
| 2835 | 2835 | const w = o.writer(); |
| 2836 | 2836 | if (!is_global) try w.writeAll("static "); |
| 2837 | 2837 | if (mod.intern_pool.stringToSliceUnwrap(link_section)) |s| |
| ... | ... | @@ -2848,7 +2848,7 @@ pub fn genHeader(dg: *DeclGen) error{ AnalysisFail, OutOfMemory }!void { |
| 2848 | 2848 | defer tracy.end(); |
| 2849 | 2849 | |
| 2850 | 2850 | const mod = dg.module; |
| 2851 | | const decl_index = dg.decl_index.unwrap().?; |
| 2851 | const decl_index = dg.pass.decl; |
| 2852 | 2852 | const decl = mod.declPtr(decl_index); |
| 2853 | 2853 | const tv: TypedValue = .{ |
| 2854 | 2854 | .ty = decl.ty, |
| ... | ... | @@ -2861,7 +2861,7 @@ pub fn genHeader(dg: *DeclGen) error{ AnalysisFail, OutOfMemory }!void { |
| 2861 | 2861 | const is_global = dg.declIsGlobal(tv); |
| 2862 | 2862 | if (is_global) { |
| 2863 | 2863 | try writer.writeAll("zig_extern "); |
| 2864 | | try dg.renderFunctionSignature(writer, dg.decl_index.unwrap().?, .complete, .{ .export_index = 0 }); |
| 2864 | try dg.renderFunctionSignature(writer, dg.pass.decl, .complete, .{ .export_index = 0 }); |
| 2865 | 2865 | try dg.fwd_decl.appendSlice(";\n"); |
| 2866 | 2866 | } |
| 2867 | 2867 | }, |
| ... | ... | @@ -7279,7 +7279,7 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7279 | 7279 | fn airCVaStart(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7280 | 7280 | const mod = f.object.dg.module; |
| 7281 | 7281 | const inst_ty = f.typeOfIndex(inst); |
| 7282 | | const decl_index = f.object.dg.decl_index.unwrap().?; |
| 7282 | const decl_index = f.object.dg.pass.decl; |
| 7283 | 7283 | const decl = mod.declPtr(decl_index); |
| 7284 | 7284 | const fn_cty = try f.typeToCType(decl.ty, .complete); |
| 7285 | 7285 | const param_len = fn_cty.castTag(.varargs_function).?.data.param_types.len; |