aboutsummaryrefslogtreecommitdiff
path: root/src/Sema.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2024-01-02 14:11:27 -0800
committerGitHub <noreply@github.com>2024-01-02 14:11:27 -0800
commit289ae45c1b58e952867c4fa1e246d0ef7bc2ff64 (patch)
tree5dd034143a2354b7b44496e684f1c764e2f9664c /src/Sema.zig
parentc89bb3e141ee215add0b52930d48bffd8dae8342 (diff)
parentc546ddb3edc557fae4b932e5239b9dcb66117832 (diff)
downloadzig-289ae45c1b58e952867c4fa1e246d0ef7bc2ff64.tar.gz
zig-289ae45c1b58e952867c4fa1e246d0ef7bc2ff64.zip
Merge pull request #18160 from ziglang/std-build-module
Move many settings from being per-Compilation to being per-Module
Diffstat (limited to 'src/Sema.zig')
-rw-r--r--src/Sema.zig149
1 files changed, 88 insertions, 61 deletions
diff --git a/src/Sema.zig b/src/Sema.zig
index e9af6d1740..0c464a0bc1 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -784,6 +784,11 @@ pub const Block = struct {
}
}
+ pub fn ownerModule(block: Block) *Package.Module {
+ const zcu = block.sema.mod;
+ return zcu.namespacePtr(block.namespace).file_scope.mod;
+ }
+
pub fn startAnonDecl(block: *Block) !WipAnonDecl {
return WipAnonDecl{
.block = block,
@@ -1324,13 +1329,13 @@ fn analyzeBodyInner(
},
.dbg_block_begin => {
dbg_block_begins += 1;
- try sema.zirDbgBlockBegin(block);
+ try zirDbgBlockBegin(block);
i += 1;
continue;
},
.dbg_block_end => {
dbg_block_begins -= 1;
- try sema.zirDbgBlockEnd(block);
+ try zirDbgBlockEnd(block);
i += 1;
continue;
},
@@ -1825,17 +1830,17 @@ fn analyzeBodyInner(
};
// balance out dbg_block_begins in case of early noreturn
- const noreturn_inst = block.instructions.popOrNull();
- while (dbg_block_begins > 0) {
- dbg_block_begins -= 1;
- if (block.is_comptime or mod.comp.bin_file.options.strip) continue;
-
- _ = try block.addInst(.{
- .tag = .dbg_block_end,
- .data = undefined,
- });
+ if (!block.is_comptime and !block.ownerModule().strip) {
+ const noreturn_inst = block.instructions.popOrNull();
+ while (dbg_block_begins > 0) {
+ dbg_block_begins -= 1;
+ _ = try block.addInst(.{
+ .tag = .dbg_block_end,
+ .data = undefined,
+ });
+ }
+ if (noreturn_inst) |some| try block.instructions.append(sema.gpa, some);
}
- if (noreturn_inst) |some| try block.instructions.append(sema.gpa, some);
// We may have overwritten the capture scope due to a `repeat` instruction where
// the body had a capture; restore it now.
@@ -2040,9 +2045,10 @@ fn analyzeAsType(
pub fn setupErrorReturnTrace(sema: *Sema, block: *Block, last_arg_index: usize) !void {
const mod = sema.mod;
+ const comp = mod.comp;
const gpa = sema.gpa;
const ip = &mod.intern_pool;
- if (!mod.backendSupportsFeature(.error_return_trace)) return;
+ if (!comp.config.any_error_tracing) return;
assert(!block.is_comptime);
var err_trace_block = block.makeSubBlock();
@@ -5733,7 +5739,7 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
// Ignore the result, all the relevant operations have written to c_import_buf already.
_ = try sema.analyzeBodyBreak(&child_block, body);
- var c_import_res = comp.cImport(c_import_buf.items) catch |err|
+ var c_import_res = comp.cImport(c_import_buf.items, parent_block.ownerModule()) catch |err|
return sema.fail(&child_block, src, "C import failed: {s}", .{@errorName(err)});
defer c_import_res.deinit(gpa);
@@ -5742,7 +5748,7 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
const msg = try sema.errMsg(&child_block, src, "C import failed", .{});
errdefer msg.destroy(gpa);
- if (!comp.bin_file.options.link_libc)
+ if (!comp.config.link_libc)
try sema.errNote(&child_block, src, msg, "libc headers not available; compilation does not link against libc", .{});
const gop = try mod.cimport_errors.getOrPut(gpa, sema.owner_decl_index);
@@ -5754,14 +5760,39 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
};
return sema.failWithOwnedErrorMsg(&child_block, msg);
}
- const c_import_mod = try Package.Module.create(comp.arena.allocator(), .{
- .root = .{
- .root_dir = Compilation.Directory.cwd(),
- .sub_path = std.fs.path.dirname(c_import_res.out_zig_path) orelse "",
+ const parent_mod = parent_block.ownerModule();
+ const c_import_mod = Package.Module.create(comp.arena, .{
+ .global_cache_directory = comp.global_cache_directory,
+ .paths = .{
+ .root = .{
+ .root_dir = Compilation.Directory.cwd(),
+ .sub_path = std.fs.path.dirname(c_import_res.out_zig_path) orelse "",
+ },
+ .root_src_path = std.fs.path.basename(c_import_res.out_zig_path),
},
- .root_src_path = std.fs.path.basename(c_import_res.out_zig_path),
.fully_qualified_name = c_import_res.out_zig_path,
- });
+ .cc_argv = parent_mod.cc_argv,
+ .inherited = .{},
+ .global = comp.config,
+ .parent = parent_mod,
+ .builtin_mod = parent_mod.getBuiltinDependency(),
+ }) catch |err| switch (err) {
+ // None of these are possible because we are creating a package with
+ // the exact same configuration as the parent package, which already
+ // passed these checks.
+ error.ValgrindUnsupportedOnTarget => unreachable,
+ error.TargetRequiresSingleThreaded => unreachable,
+ error.BackendRequiresSingleThreaded => unreachable,
+ error.TargetRequiresPic => unreachable,
+ error.PieRequiresPic => unreachable,
+ error.DynamicLinkingRequiresPic => unreachable,
+ error.TargetHasNoRedZone => unreachable,
+ error.StackCheckUnsupportedByTarget => unreachable,
+ error.StackProtectorUnsupportedByTarget => unreachable,
+ error.StackProtectorUnavailableWithoutLibC => unreachable,
+
+ else => |e| return e,
+ };
const result = mod.importPkg(c_import_mod) catch |err|
return sema.fail(&child_block, src, "C import failed: {s}", .{@errorName(err)});
@@ -6267,7 +6298,7 @@ fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!voi
// ZIR code that possibly will need to generate runtime code. So error messages
// and other source locations must not rely on sema.src being set from dbg_stmt
// instructions.
- if (block.is_comptime or sema.mod.comp.bin_file.options.strip) return;
+ if (block.is_comptime or block.ownerModule().strip) return;
const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt;
@@ -6292,8 +6323,8 @@ fn zirDbgStmt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!voi
});
}
-fn zirDbgBlockBegin(sema: *Sema, block: *Block) CompileError!void {
- if (block.is_comptime or sema.mod.comp.bin_file.options.strip) return;
+fn zirDbgBlockBegin(block: *Block) CompileError!void {
+ if (block.is_comptime or block.ownerModule().strip) return;
_ = try block.addInst(.{
.tag = .dbg_block_begin,
@@ -6301,8 +6332,8 @@ fn zirDbgBlockBegin(sema: *Sema, block: *Block) CompileError!void {
});
}
-fn zirDbgBlockEnd(sema: *Sema, block: *Block) CompileError!void {
- if (block.is_comptime or sema.mod.comp.bin_file.options.strip) return;
+fn zirDbgBlockEnd(block: *Block) CompileError!void {
+ if (block.is_comptime or block.ownerModule().strip) return;
_ = try block.addInst(.{
.tag = .dbg_block_end,
@@ -6316,7 +6347,7 @@ fn zirDbgVar(
inst: Zir.Inst.Index,
air_tag: Air.Inst.Tag,
) CompileError!void {
- if (block.is_comptime or sema.mod.comp.bin_file.options.strip) return;
+ if (block.is_comptime or block.ownerModule().strip) return;
const str_op = sema.code.instructions.items(.data)[@intFromEnum(inst)].str_op;
const operand = try sema.resolveInst(str_op.operand);
@@ -6513,8 +6544,7 @@ pub fn analyzeSaveErrRetIndex(sema: *Sema, block: *Block) SemaError!Air.Inst.Ref
const gpa = sema.gpa;
const src = sema.src;
- if (!mod.backendSupportsFeature(.error_return_trace)) return .none;
- if (!mod.comp.bin_file.options.error_return_tracing) return .none;
+ if (!block.ownerModule().error_tracing) return .none;
if (block.is_comptime)
return .none;
@@ -6698,7 +6728,7 @@ fn zirCall(
input_is_error = false;
}
- if (mod.backendSupportsFeature(.error_return_trace) and mod.comp.bin_file.options.error_return_tracing and
+ if (block.ownerModule().error_tracing and
!block.is_comptime and !block.is_typeof and (input_is_error or pop_error_return_trace))
{
const return_ty = sema.typeOf(call_inst);
@@ -7451,7 +7481,7 @@ fn analyzeCall(
new_fn_info.return_type = sema.fn_ret_ty.toIntern();
const new_func_resolved_ty = try mod.funcType(new_fn_info);
if (!is_comptime_call and !block.is_typeof) {
- try sema.emitDbgInline(block, prev_fn_index, module_fn_index, new_func_resolved_ty, .dbg_inline_begin);
+ try emitDbgInline(block, prev_fn_index, module_fn_index, new_func_resolved_ty, .dbg_inline_begin);
const zir_tags = sema.code.instructions.items(.tag);
for (fn_info.param_body) |param| switch (zir_tags[@intFromEnum(param)]) {
@@ -7489,7 +7519,7 @@ fn analyzeCall(
if (!is_comptime_call and !block.is_typeof and
sema.typeOf(result).zigTypeTag(mod) != .NoReturn)
{
- try sema.emitDbgInline(
+ try emitDbgInline(
block,
module_fn_index,
prev_fn_index,
@@ -8062,15 +8092,13 @@ fn resolveTupleLazyValues(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type)
}
fn emitDbgInline(
- sema: *Sema,
block: *Block,
old_func: InternPool.Index,
new_func: InternPool.Index,
new_func_ty: Type,
tag: Air.Inst.Tag,
) CompileError!void {
- const mod = sema.mod;
- if (mod.comp.bin_file.options.strip) return;
+ if (block.ownerModule().strip) return;
// Recursive inline call; no dbg_inline needed.
if (old_func == new_func) return;
@@ -9058,7 +9086,7 @@ fn resolveGenericBody(
/// Given a library name, examines if the library name should end up in
/// `link.File.Options.system_libs` table (for example, libc is always
-/// specified via dedicated flag `link.File.Options.link_libc` instead),
+/// specified via dedicated flag `link_libc` instead),
/// and puts it there if it doesn't exist.
/// It also dupes the library name which can then be saved as part of the
/// respective `Decl` (either `ExternFn` or `Var`).
@@ -9075,8 +9103,8 @@ fn handleExternLibName(
const comp = mod.comp;
const target = mod.getTarget();
log.debug("extern fn symbol expected in lib '{s}'", .{lib_name});
- if (target_util.is_libc_lib_name(target, lib_name)) {
- if (!comp.bin_file.options.link_libc) {
+ if (target.is_libc_lib_name(lib_name)) {
+ if (!comp.config.link_libc) {
return sema.fail(
block,
src_loc,
@@ -9086,22 +9114,25 @@ fn handleExternLibName(
}
break :blk;
}
- if (target_util.is_libcpp_lib_name(target, lib_name)) {
- if (!comp.bin_file.options.link_libcpp) {
- return sema.fail(
- block,
- src_loc,
- "dependency on libc++ must be explicitly specified in the build command",
- .{},
- );
- }
+ if (target.is_libcpp_lib_name(lib_name)) {
+ if (!comp.config.link_libcpp) return sema.fail(
+ block,
+ src_loc,
+ "dependency on libc++ must be explicitly specified in the build command",
+ .{},
+ );
break :blk;
}
if (mem.eql(u8, lib_name, "unwind")) {
- comp.bin_file.options.link_libunwind = true;
+ if (!comp.config.link_libunwind) return sema.fail(
+ block,
+ src_loc,
+ "dependency on libunwind must be explicitly specified in the build command",
+ .{},
+ );
break :blk;
}
- if (!target.isWasm() and !comp.bin_file.options.pic) {
+ if (!target.isWasm() and !block.ownerModule().pic) {
return sema.fail(
block,
src_loc,
@@ -18728,18 +18759,14 @@ fn retWithErrTracing(
fn wantErrorReturnTracing(sema: *Sema, fn_ret_ty: Type) bool {
const mod = sema.mod;
- if (!mod.backendSupportsFeature(.error_return_trace)) return false;
-
- return fn_ret_ty.isError(mod) and
- mod.comp.bin_file.options.error_return_tracing;
+ return fn_ret_ty.isError(mod) and mod.comp.config.any_error_tracing;
}
fn zirSaveErrRetIndex(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void {
const mod = sema.mod;
const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].save_err_ret_index;
- if (!mod.backendSupportsFeature(.error_return_trace)) return;
- if (!mod.comp.bin_file.options.error_return_tracing) return;
+ if (!block.ownerModule().error_tracing) return;
// This is only relevant at runtime.
if (block.is_comptime or block.is_typeof) return;
@@ -18764,9 +18791,8 @@ fn zirRestoreErrRetIndex(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index)
const mod = sema.mod;
const ip = &mod.intern_pool;
- if (!mod.backendSupportsFeature(.error_return_trace)) return;
if (!ip.funcAnalysis(sema.owner_func_index).calls_or_awaits_errorable_fn) return;
- if (!mod.comp.bin_file.options.error_return_tracing) return;
+ if (!start_block.ownerModule().error_tracing) return;
const tracy = trace(@src());
defer tracy.end();
@@ -20037,8 +20063,7 @@ fn getErrorReturnTrace(sema: *Sema, block: *Block) CompileError!Air.Inst.Ref {
if (sema.owner_func_index != .none and
ip.funcAnalysis(sema.owner_func_index).calls_or_awaits_errorable_fn and
- mod.comp.bin_file.options.error_return_tracing and
- mod.backendSupportsFeature(.error_return_trace))
+ block.ownerModule().error_tracing)
{
return block.addTy(.err_return_trace, opt_ptr_stack_trace_ty);
}
@@ -34520,7 +34545,9 @@ pub fn resolveFnTypes(sema: *Sema, fn_ty: Type) CompileError!void {
try sema.resolveTypeFully(Type.fromInterned(fn_ty_info.return_type));
- if (mod.comp.bin_file.options.error_return_tracing and Type.fromInterned(fn_ty_info.return_type).isError(mod)) {
+ if (mod.comp.config.any_error_tracing and
+ Type.fromInterned(fn_ty_info.return_type).isError(mod))
+ {
// Ensure the type exists so that backends can assume that.
_ = try sema.getBuiltinType("StackTrace");
}
@@ -36692,7 +36719,7 @@ fn getBuiltinDecl(sema: *Sema, block: *Block, name: []const u8) CompileError!Int
const mod = sema.mod;
const ip = &mod.intern_pool;
- const std_mod = mod.main_mod.deps.get("std").?;
+ const std_mod = mod.std_mod;
const std_file = (mod.importPkg(std_mod) catch unreachable).file;
const opt_builtin_inst = (try sema.namespaceLookupRef(
block,