aboutsummaryrefslogtreecommitdiff
path: root/src/Sema
diff options
context:
space:
mode:
authormlugg <mlugg@mlugg.co.uk>2024-07-28 17:09:14 +0100
committermlugg <mlugg@mlugg.co.uk>2024-08-11 07:29:41 +0100
commit548a087fafeda5b07d2237d5137906b8d07da699 (patch)
tree69135f129b84ab5b65f443d0a52899b232696e2b /src/Sema
parent531cd177e89c1edfcd2e52f74f220eb186a25f78 (diff)
downloadzig-548a087fafeda5b07d2237d5137906b8d07da699.tar.gz
zig-548a087fafeda5b07d2237d5137906b8d07da699.zip
compiler: split Decl into Nav and Cau
The type `Zcu.Decl` in the compiler is problematic: over time it has gained many responsibilities. Every source declaration, container type, generic instantiation, and `@extern` has a `Decl`. The functions of these `Decl`s are in some cases entirely disjoint. After careful analysis, I determined that the two main responsibilities of `Decl` are as follows: * A `Decl` acts as the "subject" of semantic analysis at comptime. A single unit of analysis is either a runtime function body, or a `Decl`. It registers incremental dependencies, tracks analysis errors, etc. * A `Decl` acts as a "global variable": a pointer to it is consistent, and it may be lowered to a specific symbol by the codegen backend. This commit eliminates `Decl` and introduces new types to model these responsibilities: `Cau` (Comptime Analysis Unit) and `Nav` (Named Addressable Value). Every source declaration, and every container type requiring resolution (so *not* including `opaque`), has a `Cau`. For a source declaration, this `Cau` performs the resolution of its value. (When #131 is implemented, it is unsolved whether type and value resolution will share a `Cau` or have two distinct `Cau`s.) For a type, this `Cau` is the context in which type resolution occurs. Every non-`comptime` source declaration, every generic instantiation, and every distinct `extern` has a `Nav`. These are sent to codegen/link: the backends by definition do not care about `Cau`s. This commit has some minor technically-breaking changes surrounding `usingnamespace`. I don't think they'll impact anyone, since the changes are fixes around semantics which were previously inconsistent (the behavior changed depending on hashmap iteration order!). Aside from that, this changeset has no significant user-facing changes. Instead, it is an internal refactor which makes it easier to correctly model the responsibilities of different objects, particularly regarding incremental compilation. The performance impact should be negligible, but I will take measurements before merging this work into `master`. Co-authored-by: Jacob Young <jacobly0@users.noreply.github.com> Co-authored-by: Jakub Konka <kubkon@jakubkonka.com>
Diffstat (limited to 'src/Sema')
-rw-r--r--src/Sema/bitcast.zig2
-rw-r--r--src/Sema/comptime_ptr_access.zig24
2 files changed, 17 insertions, 9 deletions
diff --git a/src/Sema/bitcast.zig b/src/Sema/bitcast.zig
index c5155dec63..065de877e2 100644
--- a/src/Sema/bitcast.zig
+++ b/src/Sema/bitcast.zig
@@ -254,7 +254,7 @@ const UnpackValueBits = struct {
.error_set_type,
.inferred_error_set_type,
.variable,
- .extern_func,
+ .@"extern",
.func,
.err,
.error_union,
diff --git a/src/Sema/comptime_ptr_access.zig b/src/Sema/comptime_ptr_access.zig
index b7d660c426..8f0b8b1b17 100644
--- a/src/Sema/comptime_ptr_access.zig
+++ b/src/Sema/comptime_ptr_access.zig
@@ -217,15 +217,23 @@ fn loadComptimePtrInner(
};
const base_val: MutableValue = switch (ptr.base_addr) {
- .decl => |decl_index| val: {
- try sema.declareDependency(.{ .decl_val = decl_index });
- try sema.ensureDeclAnalyzed(decl_index);
- const decl = zcu.declPtr(decl_index);
- if (decl.val.getVariable(zcu) != null) return .runtime_load;
- break :val .{ .interned = decl.val.toIntern() };
+ .nav => |nav| val: {
+ try sema.declareDependency(.{ .nav_val = nav });
+ try sema.ensureNavResolved(src, nav);
+ const val = ip.getNav(nav).status.resolved.val;
+ switch (ip.indexToKey(val)) {
+ .variable => return .runtime_load,
+ // We let `.@"extern"` through here if it's a function.
+ // This allows you to alias `extern fn`s.
+ .@"extern" => |e| if (Type.fromInterned(e.ty).zigTypeTag(zcu) == .Fn)
+ break :val .{ .interned = val }
+ else
+ return .runtime_load,
+ else => break :val .{ .interned = val },
+ }
},
.comptime_alloc => |alloc_index| sema.getComptimeAlloc(alloc_index).val,
- .anon_decl => |anon_decl| .{ .interned = anon_decl.val },
+ .uav => |uav| .{ .interned = uav.val },
.comptime_field => |val| .{ .interned = val },
.int => return .runtime_load,
.eu_payload => |base_ptr_ip| val: {
@@ -580,7 +588,7 @@ fn prepareComptimePtrStore(
// `base_strat` will not be an error case.
const base_strat: ComptimeStoreStrategy = switch (ptr.base_addr) {
- .decl, .anon_decl, .int => return .runtime_store,
+ .nav, .uav, .int => return .runtime_store,
.comptime_field => return .comptime_field,
.comptime_alloc => |alloc_index| .{ .direct = .{
.alloc = alloc_index,