Skip to content

Commit c632fbe

Browse files
committed
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 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`.
1 parent ddcb7b1 commit c632fbe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+6272
-7046
lines changed

src/Compilation.zig

Lines changed: 76 additions & 151 deletions
Large diffs are not rendered by default.

src/InternPool.zig

Lines changed: 921 additions & 459 deletions
Large diffs are not rendered by default.

src/Sema.zig

Lines changed: 1002 additions & 1044 deletions
Large diffs are not rendered by default.

src/Sema/bitcast.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ const UnpackValueBits = struct {
254254
.error_set_type,
255255
.inferred_error_set_type,
256256
.variable,
257-
.extern_func,
257+
.@"extern",
258258
.func,
259259
.err,
260260
.error_union,

src/Sema/comptime_ptr_access.zig

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,23 @@ fn loadComptimePtrInner(
217217
};
218218

219219
const base_val: MutableValue = switch (ptr.base_addr) {
220-
.decl => |decl_index| val: {
221-
try sema.declareDependency(.{ .decl_val = decl_index });
222-
try sema.ensureDeclAnalyzed(decl_index);
223-
const decl = zcu.declPtr(decl_index);
224-
if (decl.val.getVariable(zcu) != null) return .runtime_load;
225-
break :val .{ .interned = decl.val.toIntern() };
220+
.nav => |nav| val: {
221+
try sema.declareDependency(.{ .nav_val = nav });
222+
try sema.ensureNavResolved(src, nav);
223+
const val = ip.getNav(nav).status.resolved.val;
224+
switch (ip.indexToKey(val)) {
225+
.variable => return .runtime_load,
226+
// We let `.@"extern"` through here if it's a function.
227+
// This allows you to alias `extern fn`s.
228+
.@"extern" => |e| if (Type.fromInterned(e.ty).zigTypeTag(zcu) == .Fn)
229+
break :val .{ .interned = val }
230+
else
231+
return .runtime_load,
232+
else => break :val .{ .interned = val },
233+
}
226234
},
227235
.comptime_alloc => |alloc_index| sema.getComptimeAlloc(alloc_index).val,
228-
.anon_decl => |anon_decl| .{ .interned = anon_decl.val },
236+
.uav => |uav| .{ .interned = uav.val },
229237
.comptime_field => |val| .{ .interned = val },
230238
.int => return .runtime_load,
231239
.eu_payload => |base_ptr_ip| val: {
@@ -580,7 +588,7 @@ fn prepareComptimePtrStore(
580588

581589
// `base_strat` will not be an error case.
582590
const base_strat: ComptimeStoreStrategy = switch (ptr.base_addr) {
583-
.decl, .anon_decl, .int => return .runtime_store,
591+
.nav, .uav, .int => return .runtime_store,
584592
.comptime_field => return .comptime_field,
585593
.comptime_alloc => |alloc_index| .{ .direct = .{
586594
.alloc = alloc_index,

0 commit comments

Comments
 (0)