| ... | @@ -5732,6 +5732,8 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr | ... | @@ -5732,6 +5732,8 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr |
| 5732 | const tracy = trace(@src()); | 5732 | const tracy = trace(@src()); |
| 5733 | defer tracy.end(); | 5733 | defer tracy.end(); |
| 5734 | | 5734 | |
| | 5735 | const mod = sema.mod; |
| | 5736 | const gpa = sema.gpa; |
| 5735 | const pl_node = sema.code.instructions.items(.data)[inst].pl_node; | 5737 | const pl_node = sema.code.instructions.items(.data)[inst].pl_node; |
| 5736 | const src = pl_node.src(); | 5738 | const src = pl_node.src(); |
| 5737 | const extra = sema.code.extraData(Zir.Inst.Block, pl_node.payload_index); | 5739 | const extra = sema.code.extraData(Zir.Inst.Block, pl_node.payload_index); |
| ... | @@ -5741,7 +5743,7 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr | ... | @@ -5741,7 +5743,7 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr |
| 5741 | if (!@import("build_options").have_llvm) | 5743 | if (!@import("build_options").have_llvm) |
| 5742 | return sema.fail(parent_block, src, "C import unavailable; Zig compiler built without LLVM extensions", .{}); | 5744 | return sema.fail(parent_block, src, "C import unavailable; Zig compiler built without LLVM extensions", .{}); |
| 5743 | | 5745 | |
| 5744 | var c_import_buf = std.ArrayList(u8).init(sema.gpa); | 5746 | var c_import_buf = std.ArrayList(u8).init(gpa); |
| 5745 | defer c_import_buf.deinit(); | 5747 | defer c_import_buf.deinit(); |
| 5746 | | 5748 | |
| 5747 | var comptime_reason: Block.ComptimeReason = .{ .c_import = .{ | 5749 | var comptime_reason: Block.ComptimeReason = .{ .c_import = .{ |
| ... | @@ -5763,25 +5765,24 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr | ... | @@ -5763,25 +5765,24 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr |
| 5763 | .runtime_loop = parent_block.runtime_loop, | 5765 | .runtime_loop = parent_block.runtime_loop, |
| 5764 | .runtime_index = parent_block.runtime_index, | 5766 | .runtime_index = parent_block.runtime_index, |
| 5765 | }; | 5767 | }; |
| 5766 | defer child_block.instructions.deinit(sema.gpa); | 5768 | defer child_block.instructions.deinit(gpa); |
| 5767 | | 5769 | |
| 5768 | // Ignore the result, all the relevant operations have written to c_import_buf already. | 5770 | // Ignore the result, all the relevant operations have written to c_import_buf already. |
| 5769 | _ = try sema.analyzeBodyBreak(&child_block, body); | 5771 | _ = try sema.analyzeBodyBreak(&child_block, body); |
| 5770 | | 5772 | |
| 5771 | const mod = sema.mod; | | |
| 5772 | var c_import_res = mod.comp.cImport(c_import_buf.items) catch |err| | 5773 | var c_import_res = mod.comp.cImport(c_import_buf.items) catch |err| |
| 5773 | return sema.fail(&child_block, src, "C import failed: {s}", .{@errorName(err)}); | 5774 | return sema.fail(&child_block, src, "C import failed: {s}", .{@errorName(err)}); |
| 5774 | defer c_import_res.deinit(mod.comp.gpa); | 5775 | defer c_import_res.deinit(gpa); |
| 5775 | | 5776 | |
| 5776 | if (c_import_res.errors.errorMessageCount() != 0) { | 5777 | if (c_import_res.errors.errorMessageCount() != 0) { |
| 5777 | const msg = msg: { | 5778 | const msg = msg: { |
| 5778 | const msg = try sema.errMsg(&child_block, src, "C import failed", .{}); | 5779 | const msg = try sema.errMsg(&child_block, src, "C import failed", .{}); |
| 5779 | errdefer msg.destroy(sema.gpa); | 5780 | errdefer msg.destroy(gpa); |
| 5780 | | 5781 | |
| 5781 | if (!mod.comp.bin_file.options.link_libc) | 5782 | if (!mod.comp.bin_file.options.link_libc) |
| 5782 | try sema.errNote(&child_block, src, msg, "libc headers not available; compilation does not link against libc", .{}); | 5783 | try sema.errNote(&child_block, src, msg, "libc headers not available; compilation does not link against libc", .{}); |
| 5783 | | 5784 | |
| 5784 | const gop = try mod.cimport_errors.getOrPut(sema.gpa, sema.owner_decl_index); | 5785 | const gop = try mod.cimport_errors.getOrPut(gpa, sema.owner_decl_index); |
| 5785 | if (!gop.found_existing) { | 5786 | if (!gop.found_existing) { |
| 5786 | gop.value_ptr.* = c_import_res.errors; | 5787 | gop.value_ptr.* = c_import_res.errors; |
| 5787 | c_import_res.errors = std.zig.ErrorBundle.empty; | 5788 | c_import_res.errors = std.zig.ErrorBundle.empty; |
| ... | @@ -5790,16 +5791,19 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr | ... | @@ -5790,16 +5791,19 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr |
| 5790 | }; | 5791 | }; |
| 5791 | return sema.failWithOwnedErrorMsg(&child_block, msg); | 5792 | return sema.failWithOwnedErrorMsg(&child_block, msg); |
| 5792 | } | 5793 | } |
| 5793 | const c_import_pkg = Package.create( | 5794 | // All modules are intended to go into an arena with a lifetime >= the ZigUnit. |
| 5794 | sema.gpa, | 5795 | // After the other uses of `tmp_hack_arena` are eliminated, it should be |
| 5795 | null, | 5796 | // renamed to something more appropriate such as simply `arena`. |
| 5796 | c_import_res.out_zig_path, | 5797 | const zu_arena = mod.tmp_hack_arena.allocator(); |
| 5797 | ) catch |err| switch (err) { | 5798 | const c_import_mod = try Package.Module.create(zu_arena, .{ |
| 5798 | error.OutOfMemory => return error.OutOfMemory, | 5799 | .root = .{ |
| 5799 | else => unreachable, // we pass null for root_src_dir_path | 5800 | .root_dir = Compilation.Directory.cwd(), |
| 5800 | }; | 5801 | .sub_path = std.fs.path.dirname(c_import_res.out_zig_path) orelse "", |
| | 5802 | }, |
| | 5803 | .root_src_path = std.fs.path.basename(c_import_res.out_zig_path), |
| | 5804 | }); |
| 5801 | | 5805 | |
| 5802 | const result = mod.importPkg(c_import_pkg) catch |err| | 5806 | const result = mod.importPkg(c_import_mod) catch |err| |
| 5803 | return sema.fail(&child_block, src, "C import failed: {s}", .{@errorName(err)}); | 5807 | return sema.fail(&child_block, src, "C import failed: {s}", .{@errorName(err)}); |
| 5804 | | 5808 | |
| 5805 | mod.astGenFile(result.file) catch |err| | 5809 | mod.astGenFile(result.file) catch |err| |