aboutsummaryrefslogtreecommitdiff
path: root/src/type.zig
diff options
context:
space:
mode:
authorJacob Young <jacobly0@users.noreply.github.com>2023-06-20 14:02:04 -0400
committerJacob Young <jacobly0@users.noreply.github.com>2023-06-20 14:02:09 -0400
commit96cdd51c14337b012910a36ba93121a371509cfd (patch)
tree9b75389283a2446a2fbc6450e5755faae6410851 /src/type.zig
parent52ec121469d7bf10116fb22c122cbce8ceddd028 (diff)
downloadzig-96cdd51c14337b012910a36ba93121a371509cfd.tar.gz
zig-96cdd51c14337b012910a36ba93121a371509cfd.zip
Type: delete legacy allocation functions
Diffstat (limited to 'src/type.zig')
-rw-r--r--src/type.zig210
1 files changed, 35 insertions, 175 deletions
diff --git a/src/type.zig b/src/type.zig
index 32ea8e2c5d..1debc9ab38 100644
--- a/src/type.zig
+++ b/src/type.zig
@@ -2,7 +2,6 @@ const std = @import("std");
const builtin = @import("builtin");
const Value = @import("value.zig").Value;
const assert = std.debug.assert;
-const Allocator = std.mem.Allocator;
const Target = std.Target;
const Module = @import("Module.zig");
const log = std.log.scoped(.Type);
@@ -102,10 +101,10 @@ pub const Type = struct {
};
}
- pub fn ptrInfoIp(ip: *const InternPool, ty: InternPool.Index) InternPool.Key.PtrType {
- return switch (ip.indexToKey(ty)) {
+ pub fn ptrInfo(ty: Type, mod: *const Module) InternPool.Key.PtrType {
+ return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
.ptr_type => |p| p,
- .opt_type => |child| switch (ip.indexToKey(child)) {
+ .opt_type => |child| switch (mod.intern_pool.indexToKey(child)) {
.ptr_type => |p| p,
else => unreachable,
},
@@ -113,10 +112,6 @@ pub const Type = struct {
};
}
- pub fn ptrInfo(ty: Type, mod: *const Module) Payload.Pointer.Data {
- return Payload.Pointer.Data.fromKey(ptrInfoIp(&mod.intern_pool, ty.toIntern()));
- }
-
pub fn eql(a: Type, b: Type, mod: *const Module) bool {
_ = mod; // TODO: remove this parameter
// The InternPool data structure hashes based on Key to make interned objects
@@ -181,15 +176,6 @@ pub const Type = struct {
return writer.print("{any}", .{start_type.ip_index});
}
- pub const nameAllocArena = nameAlloc;
-
- pub fn nameAlloc(ty: Type, ally: Allocator, module: *Module) Allocator.Error![:0]const u8 {
- var buffer = std.ArrayList(u8).init(ally);
- defer buffer.deinit();
- try ty.print(buffer.writer(), module);
- return buffer.toOwnedSliceSentinel(0);
- }
-
/// Prints a name suitable for `@typeName`.
pub fn print(ty: Type, writer: anytype, mod: *Module) @TypeOf(writer).Error!void {
switch (mod.intern_pool.indexToKey(ty.toIntern())) {
@@ -203,42 +189,44 @@ pub const Type = struct {
.ptr_type => {
const info = ty.ptrInfo(mod);
- if (info.sentinel) |s| switch (info.size) {
+ if (info.sentinel != .none) switch (info.flags.size) {
.One, .C => unreachable,
- .Many => try writer.print("[*:{}]", .{s.fmtValue(info.pointee_type, mod)}),
- .Slice => try writer.print("[:{}]", .{s.fmtValue(info.pointee_type, mod)}),
- } else switch (info.size) {
+ .Many => try writer.print("[*:{}]", .{info.sentinel.toValue().fmtValue(info.child.toType(), mod)}),
+ .Slice => try writer.print("[:{}]", .{info.sentinel.toValue().fmtValue(info.child.toType(), mod)}),
+ } else switch (info.flags.size) {
.One => try writer.writeAll("*"),
.Many => try writer.writeAll("[*]"),
.C => try writer.writeAll("[*c]"),
.Slice => try writer.writeAll("[]"),
}
- if (info.@"align" != 0 or info.host_size != 0 or info.vector_index != .none) {
- if (info.@"align" != 0) {
- try writer.print("align({d}", .{info.@"align"});
- } else {
- const alignment = info.pointee_type.abiAlignment(mod);
- try writer.print("align({d}", .{alignment});
- }
-
- if (info.bit_offset != 0 or info.host_size != 0) {
- try writer.print(":{d}:{d}", .{ info.bit_offset, info.host_size });
+ if (info.flags.alignment != .none or
+ info.packed_offset.host_size != 0 or
+ info.flags.vector_index != .none)
+ {
+ const alignment = info.flags.alignment.toByteUnitsOptional() orelse
+ info.child.toType().abiAlignment(mod);
+ try writer.print("align({d}", .{alignment});
+
+ if (info.packed_offset.bit_offset != 0 or info.packed_offset.host_size != 0) {
+ try writer.print(":{d}:{d}", .{
+ info.packed_offset.bit_offset, info.packed_offset.host_size,
+ });
}
- if (info.vector_index == .runtime) {
+ if (info.flags.vector_index == .runtime) {
try writer.writeAll(":?");
- } else if (info.vector_index != .none) {
- try writer.print(":{d}", .{@intFromEnum(info.vector_index)});
+ } else if (info.flags.vector_index != .none) {
+ try writer.print(":{d}", .{@intFromEnum(info.flags.vector_index)});
}
try writer.writeAll(") ");
}
- if (info.@"addrspace" != .generic) {
- try writer.print("addrspace(.{s}) ", .{@tagName(info.@"addrspace")});
+ if (info.flags.address_space != .generic) {
+ try writer.print("addrspace(.{s}) ", .{@tagName(info.flags.address_space)});
}
- if (!info.mutable) try writer.writeAll("const ");
- if (info.@"volatile") try writer.writeAll("volatile ");
- if (info.@"allowzero" and info.size != .C) try writer.writeAll("allowzero ");
+ if (info.flags.is_const) try writer.writeAll("const ");
+ if (info.flags.is_volatile) try writer.writeAll("volatile ");
+ if (info.flags.is_allowzero and info.flags.size != .C) try writer.writeAll("allowzero ");
- try print(info.pointee_type, writer, mod);
+ try print(info.child.toType(), writer, mod);
return;
},
.array_type => |array_type| {
@@ -1035,9 +1023,8 @@ pub const Type = struct {
else => |e| return e,
})) continue;
- const field_align = if (field.abi_align != 0)
- field.abi_align
- else switch (try field.ty.abiAlignmentAdvanced(mod, strat)) {
+ const field_align = @intCast(u32, field.abi_align.toByteUnitsOptional() orelse
+ switch (try field.ty.abiAlignmentAdvanced(mod, strat)) {
.scalar => |a| a,
.val => switch (strat) {
.eager => unreachable, // struct layout not resolved
@@ -1047,7 +1034,7 @@ pub const Type = struct {
.storage = .{ .lazy_align = ty.toIntern() },
} })).toValue() },
},
- };
+ });
big_align = @max(big_align, field_align);
// This logic is duplicated in Module.Struct.Field.alignment.
@@ -1242,9 +1229,8 @@ pub const Type = struct {
else => |e| return e,
})) continue;
- const field_align = if (field.abi_align != 0)
- field.abi_align
- else switch (try field.ty.abiAlignmentAdvanced(mod, strat)) {
+ const field_align = @intCast(u32, field.abi_align.toByteUnitsOptional() orelse
+ switch (try field.ty.abiAlignmentAdvanced(mod, strat)) {
.scalar => |a| a,
.val => switch (strat) {
.eager => unreachable, // struct layout not resolved
@@ -1254,7 +1240,7 @@ pub const Type = struct {
.storage = .{ .lazy_align = ty.toIntern() },
} })).toValue() },
},
- };
+ });
max_align = @max(max_align, field_align);
}
return AbiAlignmentAdvanced{ .scalar = max_align };
@@ -1883,7 +1869,7 @@ pub const Type = struct {
if (ty.isPtrLikeOptional(mod)) {
return true;
}
- return ty.ptrInfo(mod).@"allowzero";
+ return ty.ptrInfo(mod).flags.is_allowzero;
}
/// See also `isPtrLikeOptional`.
@@ -3345,58 +3331,6 @@ pub const Type = struct {
};
}
- pub const Payload = struct {
- /// TODO: remove this data structure since we have `InternPool.Key.PtrType`.
- pub const Pointer = struct {
- pub const Data = struct {
- pointee_type: Type,
- sentinel: ?Value = null,
- /// If zero use pointee_type.abiAlignment()
- /// When creating pointer types, if alignment is equal to pointee type
- /// abi alignment, this value should be set to 0 instead.
- @"align": u32 = 0,
- /// See src/target.zig defaultAddressSpace function for how to obtain
- /// an appropriate value for this field.
- @"addrspace": std.builtin.AddressSpace,
- bit_offset: u16 = 0,
- /// If this is non-zero it means the pointer points to a sub-byte
- /// range of data, which is backed by a "host integer" with this
- /// number of bytes.
- /// When host_size=pointee_abi_size and bit_offset=0, this must be
- /// represented with host_size=0 instead.
- host_size: u16 = 0,
- vector_index: VectorIndex = .none,
- @"allowzero": bool = false,
- mutable: bool = true, // TODO rename this to const, not mutable
- @"volatile": bool = false,
- size: std.builtin.Type.Pointer.Size = .One,
-
- pub const VectorIndex = InternPool.Key.PtrType.VectorIndex;
-
- pub fn alignment(data: Data, mod: *Module) u32 {
- if (data.@"align" != 0) return data.@"align";
- return abiAlignment(data.pointee_type, mod);
- }
-
- pub fn fromKey(p: InternPool.Key.PtrType) Data {
- return .{
- .pointee_type = p.child.toType(),
- .sentinel = if (p.sentinel != .none) p.sentinel.toValue() else null,
- .@"align" = @intCast(u32, p.flags.alignment.toByteUnits(0)),
- .@"addrspace" = p.flags.address_space,
- .bit_offset = p.packed_offset.bit_offset,
- .host_size = p.packed_offset.host_size,
- .vector_index = p.flags.vector_index,
- .@"allowzero" = p.flags.is_allowzero,
- .mutable = !p.flags.is_const,
- .@"volatile" = p.flags.is_volatile,
- .size = p.flags.size,
- };
- }
- };
- };
- };
-
pub const @"u1": Type = .{ .ip_index = .u1_type };
pub const @"u8": Type = .{ .ip_index = .u8_type };
pub const @"u16": Type = .{ .ip_index = .u16_type };
@@ -3454,80 +3388,6 @@ pub const Type = struct {
pub const err_int = Type.u16;
- pub fn ptr(arena: Allocator, mod: *Module, data: Payload.Pointer.Data) !Type {
- // TODO: update callsites of this function to directly call mod.ptrType
- // and then delete this function.
- _ = arena;
-
- var d = data;
-
- // Canonicalize non-zero alignment. If it matches the ABI alignment of the pointee
- // type, we change it to 0 here. If this causes an assertion trip because the
- // pointee type needs to be resolved more, that needs to be done before calling
- // this ptr() function.
- if (d.@"align" != 0) canonicalize: {
- if (!d.pointee_type.layoutIsResolved(mod)) break :canonicalize;
- if (d.@"align" == d.pointee_type.abiAlignment(mod)) {
- d.@"align" = 0;
- }
- }
-
- // Canonicalize host_size. If it matches the bit size of the pointee type,
- // we change it to 0 here. If this causes an assertion trip, the pointee type
- // needs to be resolved before calling this ptr() function.
- if (d.host_size != 0) {
- assert(d.bit_offset < d.host_size * 8);
- if (d.host_size * 8 == d.pointee_type.bitSize(mod)) {
- assert(d.bit_offset == 0);
- d.host_size = 0;
- }
- }
-
- return mod.ptrType(.{
- .child = d.pointee_type.ip_index,
- .sentinel = if (d.sentinel) |s| s.ip_index else .none,
- .flags = .{
- .alignment = InternPool.Alignment.fromByteUnits(d.@"align"),
- .vector_index = d.vector_index,
- .size = d.size,
- .is_const = !d.mutable,
- .is_volatile = d.@"volatile",
- .is_allowzero = d.@"allowzero",
- .address_space = d.@"addrspace",
- },
- .packed_offset = .{
- .host_size = d.host_size,
- .bit_offset = d.bit_offset,
- },
- });
- }
-
- pub fn array(
- arena: Allocator,
- len: u64,
- sent: ?Value,
- elem_type: Type,
- mod: *Module,
- ) Allocator.Error!Type {
- // TODO: update callsites of this function to directly call mod.arrayType
- // and then delete this function.
- _ = arena;
-
- return mod.arrayType(.{
- .len = len,
- .child = elem_type.ip_index,
- .sentinel = if (sent) |s| s.ip_index else .none,
- });
- }
-
- pub fn optional(arena: Allocator, child_type: Type, mod: *Module) Allocator.Error!Type {
- // TODO: update callsites of this function to directly call
- // mod.optionalType and then delete this function.
- _ = arena;
-
- return mod.optionalType(child_type.ip_index);
- }
-
pub fn smallestUnsignedBits(max: u64) u16 {
if (max == 0) return 0;
const base = std.math.log2(max);