authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-12-11 19:16:57-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-15 15:11:35-08:00
logc96e23632f04c7bffc2257139a19cad82fbe7223
tree793a2a41d98d67c7a46d70384d7f732a636a91e9
parent26c38b2d42ca3d68a8964c83a86a7e69f0990019

frontend: add const to more Zcu pointers


3 files changed, 115 insertions(+), 88 deletions(-)

src/InternPool.zig+5-1
......@@ -3360,6 +3360,10 @@ pub const LoadedUnionType = struct {
33603360 return flags.status == .field_types_wip;
33613361 }
33623362
3363 pub fn requiresComptime(u: LoadedUnionType, ip: *const InternPool) RequiresComptime {
3364 return u.flagsUnordered(ip).requires_comptime;
3365 }
3366
33633367 pub fn setRequiresComptimeWip(u: LoadedUnionType, ip: *InternPool) RequiresComptime {
33643368 const extra_mutex = &ip.getLocal(u.tid).mutate.extra.mutex;
33653369 extra_mutex.lock();
......@@ -4014,7 +4018,7 @@ pub const LoadedStructType = struct {
40144018 }
40154019 }
40164020
4017 pub fn haveLayout(s: LoadedStructType, ip: *InternPool) bool {
4021 pub fn haveLayout(s: LoadedStructType, ip: *const InternPool) bool {
40184022 return switch (s.layout) {
40194023 .@"packed" => s.backingIntTypeUnordered(ip) != .none,
40204024 .auto, .@"extern" => s.flagsUnordered(ip).layout_resolved,
src/Type.zig+108-85
......@@ -441,7 +441,7 @@ pub fn toValue(self: Type) Value {
441441
442442const RuntimeBitsError = SemaError || error{NeedLazy};
443443
444pub fn hasRuntimeBits(ty: Type, zcu: *Zcu) bool {
444pub fn hasRuntimeBits(ty: Type, zcu: *const Zcu) bool {
445445 return hasRuntimeBitsInner(ty, false, .eager, zcu, {}) catch unreachable;
446446}
447447
......@@ -452,7 +452,7 @@ pub fn hasRuntimeBitsSema(ty: Type, pt: Zcu.PerThread) SemaError!bool {
452452 };
453453}
454454
455pub fn hasRuntimeBitsIgnoreComptime(ty: Type, zcu: *Zcu) bool {
455pub fn hasRuntimeBitsIgnoreComptime(ty: Type, zcu: *const Zcu) bool {
456456 return hasRuntimeBitsInner(ty, true, .eager, zcu, {}) catch unreachable;
457457}
458458
......@@ -471,7 +471,7 @@ pub fn hasRuntimeBitsInner(
471471 ty: Type,
472472 ignore_comptime_only: bool,
473473 comptime strat: ResolveStratLazy,
474 zcu: *Zcu,
474 zcu: strat.ZcuPtr(),
475475 tid: strat.Tid(),
476476) RuntimeBitsError!bool {
477477 const ip = &zcu.intern_pool;
......@@ -560,7 +560,7 @@ pub fn hasRuntimeBitsInner(
560560 },
561561 .struct_type => {
562562 const struct_type = ip.loadStructType(ty.toIntern());
563 if (struct_type.assumeRuntimeBitsIfFieldTypesWip(ip)) {
563 if (strat != .eager and struct_type.assumeRuntimeBitsIfFieldTypesWip(ip)) {
564564 // In this case, we guess that hasRuntimeBits() for this type is true,
565565 // and then later if our guess was incorrect, we emit a compile error.
566566 return true;
......@@ -596,7 +596,7 @@ pub fn hasRuntimeBitsInner(
596596 const union_type = ip.loadUnionType(ty.toIntern());
597597 const union_flags = union_type.flagsUnordered(ip);
598598 switch (union_flags.runtime_tag) {
599 .none => {
599 .none => if (strat != .eager) {
600600 // In this case, we guess that hasRuntimeBits() for this type is true,
601601 // and then later if our guess was incorrect, we emit a compile error.
602602 if (union_type.assumeRuntimeBitsIfFieldTypesWip(ip)) return true;
......@@ -774,7 +774,7 @@ pub fn fnHasRuntimeBitsSema(ty: Type, pt: Zcu.PerThread) SemaError!bool {
774774pub fn fnHasRuntimeBitsInner(
775775 ty: Type,
776776 comptime strat: ResolveStrat,
777 zcu: *Zcu,
777 zcu: strat.ZcuPtr(),
778778 tid: strat.Tid(),
779779) SemaError!bool {
780780 const fn_info = zcu.typeToFunc(ty).?;
......@@ -815,7 +815,7 @@ pub fn ptrAlignmentSema(ty: Type, pt: Zcu.PerThread) SemaError!Alignment {
815815pub fn ptrAlignmentInner(
816816 ty: Type,
817817 comptime strat: ResolveStrat,
818 zcu: *Zcu,
818 zcu: strat.ZcuPtr(),
819819 tid: strat.Tid(),
820820) !Alignment {
821821 return switch (zcu.intern_pool.indexToKey(ty.toIntern())) {
......@@ -868,14 +868,25 @@ pub const ResolveStratLazy = enum {
868868 /// This should typically be used from semantic analysis.
869869 sema,
870870
871 pub fn Tid(comptime strat: ResolveStratLazy) type {
871 pub fn Tid(strat: ResolveStratLazy) type {
872872 return switch (strat) {
873873 .lazy, .sema => Zcu.PerThread.Id,
874874 .eager => void,
875875 };
876876 }
877877
878 pub fn pt(comptime strat: ResolveStratLazy, zcu: *Zcu, tid: strat.Tid()) switch (strat) {
878 pub fn ZcuPtr(strat: ResolveStratLazy) type {
879 return switch (strat) {
880 .eager => *const Zcu,
881 .sema, .lazy => *Zcu,
882 };
883 }
884
885 pub fn pt(
886 comptime strat: ResolveStratLazy,
887 zcu: strat.ZcuPtr(),
888 tid: strat.Tid(),
889 ) switch (strat) {
879890 .lazy, .sema => Zcu.PerThread,
880891 .eager => void,
881892 } {
......@@ -896,14 +907,21 @@ pub const ResolveStrat = enum {
896907 /// This should typically be used from semantic analysis.
897908 sema,
898909
899 pub fn Tid(comptime strat: ResolveStrat) type {
910 pub fn Tid(strat: ResolveStrat) type {
900911 return switch (strat) {
901912 .sema => Zcu.PerThread.Id,
902913 .normal => void,
903914 };
904915 }
905916
906 pub fn pt(comptime strat: ResolveStrat, zcu: *Zcu, tid: strat.Tid()) switch (strat) {
917 pub fn ZcuPtr(strat: ResolveStrat) type {
918 return switch (strat) {
919 .normal => *const Zcu,
920 .sema => *Zcu,
921 };
922 }
923
924 pub fn pt(comptime strat: ResolveStrat, zcu: strat.ZcuPtr(), tid: strat.Tid()) switch (strat) {
907925 .sema => Zcu.PerThread,
908926 .normal => void,
909927 } {
......@@ -922,7 +940,7 @@ pub const ResolveStrat = enum {
922940};
923941
924942/// Never returns `none`. Asserts that all necessary type resolution is already done.
925pub fn abiAlignment(ty: Type, zcu: *Zcu) Alignment {
943pub fn abiAlignment(ty: Type, zcu: *const Zcu) Alignment {
926944 return (ty.abiAlignmentInner(.eager, zcu, {}) catch unreachable).scalar;
927945}
928946
......@@ -939,7 +957,7 @@ pub fn abiAlignmentSema(ty: Type, pt: Zcu.PerThread) SemaError!Alignment {
939957pub fn abiAlignmentInner(
940958 ty: Type,
941959 comptime strat: ResolveStratLazy,
942 zcu: *Zcu,
960 zcu: strat.ZcuPtr(),
943961 tid: strat.Tid(),
944962) SemaError!AbiAlignmentInner {
945963 const pt = strat.pt(zcu, tid);
......@@ -1156,7 +1174,7 @@ pub fn abiAlignmentInner(
11561174fn abiAlignmentInnerErrorUnion(
11571175 ty: Type,
11581176 comptime strat: ResolveStratLazy,
1159 zcu: *Zcu,
1177 zcu: strat.ZcuPtr(),
11601178 tid: strat.Tid(),
11611179 payload_ty: Type,
11621180) SemaError!AbiAlignmentInner {
......@@ -1198,7 +1216,7 @@ fn abiAlignmentInnerErrorUnion(
11981216fn abiAlignmentInnerOptional(
11991217 ty: Type,
12001218 comptime strat: ResolveStratLazy,
1201 zcu: *Zcu,
1219 zcu: strat.ZcuPtr(),
12021220 tid: strat.Tid(),
12031221) SemaError!AbiAlignmentInner {
12041222 const pt = strat.pt(zcu, tid);
......@@ -1244,7 +1262,7 @@ const AbiSizeInner = union(enum) {
12441262
12451263/// Asserts the type has the ABI size already resolved.
12461264/// Types that return false for hasRuntimeBits() return 0.
1247pub fn abiSize(ty: Type, zcu: *Zcu) u64 {
1265pub fn abiSize(ty: Type, zcu: *const Zcu) u64 {
12481266 return (abiSizeInner(ty, .eager, zcu, {}) catch unreachable).scalar;
12491267}
12501268
......@@ -1269,7 +1287,7 @@ pub fn abiSizeSema(ty: Type, pt: Zcu.PerThread) SemaError!u64 {
12691287pub fn abiSizeInner(
12701288 ty: Type,
12711289 comptime strat: ResolveStratLazy,
1272 zcu: *Zcu,
1290 zcu: strat.ZcuPtr(),
12731291 tid: strat.Tid(),
12741292) SemaError!AbiSizeInner {
12751293 const target = zcu.getTarget();
......@@ -1542,7 +1560,7 @@ pub fn abiSizeInner(
15421560fn abiSizeInnerOptional(
15431561 ty: Type,
15441562 comptime strat: ResolveStratLazy,
1545 zcu: *Zcu,
1563 zcu: strat.ZcuPtr(),
15461564 tid: strat.Tid(),
15471565) SemaError!AbiSizeInner {
15481566 const child_ty = ty.optionalChild(zcu);
......@@ -1701,7 +1719,7 @@ pub fn maxIntAlignment(target: std.Target, use_llvm: bool) u16 {
17011719 };
17021720}
17031721
1704pub fn bitSize(ty: Type, zcu: *Zcu) u64 {
1722pub fn bitSize(ty: Type, zcu: *const Zcu) u64 {
17051723 return bitSizeInner(ty, .normal, zcu, {}) catch unreachable;
17061724}
17071725
......@@ -1712,7 +1730,7 @@ pub fn bitSizeSema(ty: Type, pt: Zcu.PerThread) SemaError!u64 {
17121730pub fn bitSizeInner(
17131731 ty: Type,
17141732 comptime strat: ResolveStrat,
1715 zcu: *Zcu,
1733 zcu: strat.ZcuPtr(),
17161734 tid: strat.Tid(),
17171735) SemaError!u64 {
17181736 const target = zcu.getTarget();
......@@ -2148,7 +2166,7 @@ pub fn unionBackingType(ty: Type, pt: Zcu.PerThread) !Type {
21482166 };
21492167}
21502168
2151pub fn unionGetLayout(ty: Type, zcu: *Zcu) Zcu.UnionLayout {
2169pub fn unionGetLayout(ty: Type, zcu: *const Zcu) Zcu.UnionLayout {
21522170 const union_obj = zcu.intern_pool.loadUnionType(ty.toIntern());
21532171 return Type.getUnionLayout(union_obj, zcu);
21542172}
......@@ -2746,7 +2764,7 @@ pub fn onePossibleValue(starting_type: Type, pt: Zcu.PerThread) !?Value {
27462764
27472765/// During semantic analysis, instead call `ty.comptimeOnlySema` which
27482766/// resolves field types rather than asserting they are already resolved.
2749pub fn comptimeOnly(ty: Type, zcu: *Zcu) bool {
2767pub fn comptimeOnly(ty: Type, zcu: *const Zcu) bool {
27502768 return ty.comptimeOnlyInner(.normal, zcu, {}) catch unreachable;
27512769}
27522770
......@@ -2759,7 +2777,7 @@ pub fn comptimeOnlySema(ty: Type, pt: Zcu.PerThread) SemaError!bool {
27592777pub fn comptimeOnlyInner(
27602778 ty: Type,
27612779 comptime strat: ResolveStrat,
2762 zcu: *Zcu,
2780 zcu: strat.ZcuPtr(),
27632781 tid: strat.Tid(),
27642782) SemaError!bool {
27652783 const ip = &zcu.intern_pool;
......@@ -2834,40 +2852,44 @@ pub fn comptimeOnlyInner(
28342852 if (struct_type.layout == .@"packed")
28352853 return false;
28362854
2837 // A struct with no fields is not comptime-only.
2838 return switch (struct_type.setRequiresComptimeWip(ip)) {
2839 .no, .wip => false,
2840 .yes => true,
2841 .unknown => {
2842 // Inlined `assert` so that the resolution calls below are not statically reachable.
2843 if (strat != .sema) unreachable;
2844
2845 if (struct_type.flagsUnordered(ip).field_types_wip) {
2846 struct_type.setRequiresComptime(ip, .unknown);
2847 return false;
2848 }
2855 return switch (strat) {
2856 .normal => switch (struct_type.requiresComptime(ip)) {
2857 .wip => unreachable,
2858 .no => false,
2859 .yes => true,
2860 .unknown => unreachable,
2861 },
2862 .sema => switch (struct_type.setRequiresComptimeWip(ip)) {
2863 .no, .wip => false,
2864 .yes => true,
2865 .unknown => {
2866 if (struct_type.flagsUnordered(ip).field_types_wip) {
2867 struct_type.setRequiresComptime(ip, .unknown);
2868 return false;
2869 }
28492870
2850 errdefer struct_type.setRequiresComptime(ip, .unknown);
2871 errdefer struct_type.setRequiresComptime(ip, .unknown);
28512872
2852 const pt = strat.pt(zcu, tid);
2853 try ty.resolveFields(pt);
2854
2855 for (0..struct_type.field_types.len) |i_usize| {
2856 const i: u32 = @intCast(i_usize);
2857 if (struct_type.fieldIsComptime(ip, i)) continue;
2858 const field_ty = struct_type.field_types.get(ip)[i];
2859 if (try Type.fromInterned(field_ty).comptimeOnlyInner(strat, zcu, tid)) {
2860 // Note that this does not cause the layout to
2861 // be considered resolved. Comptime-only types
2862 // still maintain a layout of their
2863 // runtime-known fields.
2864 struct_type.setRequiresComptime(ip, .yes);
2865 return true;
2873 const pt = strat.pt(zcu, tid);
2874 try ty.resolveFields(pt);
2875
2876 for (0..struct_type.field_types.len) |i_usize| {
2877 const i: u32 = @intCast(i_usize);
2878 if (struct_type.fieldIsComptime(ip, i)) continue;
2879 const field_ty = struct_type.field_types.get(ip)[i];
2880 if (try Type.fromInterned(field_ty).comptimeOnlyInner(strat, zcu, tid)) {
2881 // Note that this does not cause the layout to
2882 // be considered resolved. Comptime-only types
2883 // still maintain a layout of their
2884 // runtime-known fields.
2885 struct_type.setRequiresComptime(ip, .yes);
2886 return true;
2887 }
28662888 }
2867 }
28682889
2869 struct_type.setRequiresComptime(ip, .no);
2870 return false;
2890 struct_type.setRequiresComptime(ip, .no);
2891 return false;
2892 },
28712893 },
28722894 };
28732895 },
......@@ -2882,35 +2904,40 @@ pub fn comptimeOnlyInner(
28822904
28832905 .union_type => {
28842906 const union_type = ip.loadUnionType(ty.toIntern());
2885 switch (union_type.setRequiresComptimeWip(ip)) {
2886 .no, .wip => return false,
2887 .yes => return true,
2888 .unknown => {
2889 // Inlined `assert` so that the resolution calls below are not statically reachable.
2890 if (strat != .sema) unreachable;
2891
2892 if (union_type.flagsUnordered(ip).status == .field_types_wip) {
2893 union_type.setRequiresComptime(ip, .unknown);
2894 return false;
2895 }
2907 return switch (strat) {
2908 .normal => switch (union_type.requiresComptime(ip)) {
2909 .wip => unreachable,
2910 .no => false,
2911 .yes => true,
2912 .unknown => unreachable,
2913 },
2914 .sema => switch (union_type.setRequiresComptimeWip(ip)) {
2915 .no, .wip => return false,
2916 .yes => return true,
2917 .unknown => {
2918 if (union_type.flagsUnordered(ip).status == .field_types_wip) {
2919 union_type.setRequiresComptime(ip, .unknown);
2920 return false;
2921 }
28962922
2897 errdefer union_type.setRequiresComptime(ip, .unknown);
2923 errdefer union_type.setRequiresComptime(ip, .unknown);
28982924
2899 const pt = strat.pt(zcu, tid);
2900 try ty.resolveFields(pt);
2925 const pt = strat.pt(zcu, tid);
2926 try ty.resolveFields(pt);
29012927
2902 for (0..union_type.field_types.len) |field_idx| {
2903 const field_ty = union_type.field_types.get(ip)[field_idx];
2904 if (try Type.fromInterned(field_ty).comptimeOnlyInner(strat, zcu, tid)) {
2905 union_type.setRequiresComptime(ip, .yes);
2906 return true;
2928 for (0..union_type.field_types.len) |field_idx| {
2929 const field_ty = union_type.field_types.get(ip)[field_idx];
2930 if (try Type.fromInterned(field_ty).comptimeOnlyInner(strat, zcu, tid)) {
2931 union_type.setRequiresComptime(ip, .yes);
2932 return true;
2933 }
29072934 }
2908 }
29092935
2910 union_type.setRequiresComptime(ip, .no);
2911 return false;
2936 union_type.setRequiresComptime(ip, .no);
2937 return false;
2938 },
29122939 },
2913 }
2940 };
29142941 },
29152942
29162943 .opaque_type => false,
......@@ -3207,7 +3234,7 @@ pub fn fieldAlignmentInner(
32073234 ty: Type,
32083235 index: usize,
32093236 comptime strat: ResolveStrat,
3210 zcu: *Zcu,
3237 zcu: strat.ZcuPtr(),
32113238 tid: strat.Tid(),
32123239) SemaError!Alignment {
32133240 const ip = &zcu.intern_pool;
......@@ -3281,7 +3308,7 @@ pub fn structFieldAlignmentInner(
32813308 explicit_alignment: Alignment,
32823309 layout: std.builtin.Type.ContainerLayout,
32833310 comptime strat: Type.ResolveStrat,
3284 zcu: *Zcu,
3311 zcu: strat.ZcuPtr(),
32853312 tid: strat.Tid(),
32863313) SemaError!Alignment {
32873314 assert(layout != .@"packed");
......@@ -3323,7 +3350,7 @@ pub fn unionFieldAlignmentInner(
33233350 explicit_alignment: Alignment,
33243351 layout: std.builtin.Type.ContainerLayout,
33253352 comptime strat: Type.ResolveStrat,
3326 zcu: *Zcu,
3353 zcu: strat.ZcuPtr(),
33273354 tid: strat.Tid(),
33283355) SemaError!Alignment {
33293356 assert(layout != .@"packed");
......@@ -3392,11 +3419,7 @@ pub const FieldOffset = struct {
33923419};
33933420
33943421/// Supports structs and unions.
3395pub fn structFieldOffset(
3396 ty: Type,
3397 index: usize,
3398 zcu: *Zcu,
3399) u64 {
3422pub fn structFieldOffset(ty: Type, index: usize, zcu: *const Zcu) u64 {
34003423 const ip = &zcu.intern_pool;
34013424 switch (ip.indexToKey(ty.toIntern())) {
34023425 .struct_type => {
......@@ -3944,7 +3967,7 @@ fn resolveUnionInner(
39443967 };
39453968}
39463969
3947pub fn getUnionLayout(loaded_union: InternPool.LoadedUnionType, zcu: *Zcu) Zcu.UnionLayout {
3970pub fn getUnionLayout(loaded_union: InternPool.LoadedUnionType, zcu: *const Zcu) Zcu.UnionLayout {
39483971 const ip = &zcu.intern_pool;
39493972 assert(loaded_union.haveLayout(ip));
39503973 var most_aligned_field: u32 = undefined;
src/Zcu.zig+2-2
......@@ -3449,7 +3449,7 @@ pub fn atomicPtrAlignment(
34493449/// * `@TypeOf(.{})`
34503450/// * A struct which has no fields (`struct {}`).
34513451/// * Not a struct.
3452pub fn typeToStruct(zcu: *Zcu, ty: Type) ?InternPool.LoadedStructType {
3452pub fn typeToStruct(zcu: *const Zcu, ty: Type) ?InternPool.LoadedStructType {
34533453 if (ty.ip_index == .none) return null;
34543454 const ip = &zcu.intern_pool;
34553455 return switch (ip.indexToKey(ty.ip_index)) {
......@@ -3458,7 +3458,7 @@ pub fn typeToStruct(zcu: *Zcu, ty: Type) ?InternPool.LoadedStructType {
34583458 };
34593459}
34603460
3461pub fn typeToPackedStruct(zcu: *Zcu, ty: Type) ?InternPool.LoadedStructType {
3461pub fn typeToPackedStruct(zcu: *const Zcu, ty: Type) ?InternPool.LoadedStructType {
34623462 const s = zcu.typeToStruct(ty) orelse return null;
34633463 if (s.layout != .@"packed") return null;
34643464 return s;