aboutsummaryrefslogtreecommitdiff
path: root/src/type.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/type.zig')
-rw-r--r--src/type.zig96
1 files changed, 58 insertions, 38 deletions
diff --git a/src/type.zig b/src/type.zig
index 150ea234a0..b6728b4be0 100644
--- a/src/type.zig
+++ b/src/type.zig
@@ -1766,8 +1766,20 @@ pub const Type = extern union {
}
}
+ pub fn nameAllocArena(ty: Type, arena: Allocator) Allocator.Error![:0]const u8 {
+ return nameAllocAdvanced(ty, arena, true);
+ }
+
+ pub fn nameAlloc(ty: Type, gpa: Allocator) Allocator.Error![:0]const u8 {
+ return nameAllocAdvanced(ty, gpa, false);
+ }
+
/// Returns a name suitable for `@typeName`.
- pub fn nameAlloc(ty: Type, arena: Allocator) Allocator.Error![:0]const u8 {
+ pub fn nameAllocAdvanced(
+ ty: Type,
+ ally: Allocator,
+ is_arena: bool,
+ ) Allocator.Error![:0]const u8 {
const t = ty.tag();
switch (t) {
.inferred_alloc_const => unreachable,
@@ -1812,71 +1824,79 @@ pub const Type = extern union {
.noreturn,
.var_args_param,
.bound_fn,
- => return @tagName(t),
+ => return maybeDupe(@tagName(t), ally, is_arena),
- .enum_literal => return "@Type(.EnumLiteral)",
- .@"null" => return "@Type(.Null)",
- .@"undefined" => return "@Type(.Undefined)",
+ .enum_literal => return maybeDupe("@Type(.EnumLiteral)", ally, is_arena),
+ .@"null" => return maybeDupe("@Type(.Null)", ally, is_arena),
+ .@"undefined" => return maybeDupe("@Type(.Undefined)", ally, is_arena),
- .empty_struct, .empty_struct_literal => return "struct {}",
+ .empty_struct, .empty_struct_literal => return maybeDupe("struct {}", ally, is_arena),
.@"struct" => {
const struct_obj = ty.castTag(.@"struct").?.data;
- return try arena.dupeZ(u8, std.mem.sliceTo(struct_obj.owner_decl.name, 0));
+ return try ally.dupeZ(u8, std.mem.sliceTo(struct_obj.owner_decl.name, 0));
},
.@"union", .union_tagged => {
const union_obj = ty.cast(Payload.Union).?.data;
- return try arena.dupeZ(u8, std.mem.sliceTo(union_obj.owner_decl.name, 0));
+ return try ally.dupeZ(u8, std.mem.sliceTo(union_obj.owner_decl.name, 0));
},
.enum_full, .enum_nonexhaustive => {
const enum_full = ty.cast(Payload.EnumFull).?.data;
- return try arena.dupeZ(u8, std.mem.sliceTo(enum_full.owner_decl.name, 0));
+ return try ally.dupeZ(u8, std.mem.sliceTo(enum_full.owner_decl.name, 0));
},
.enum_simple => {
const enum_simple = ty.castTag(.enum_simple).?.data;
- return try arena.dupeZ(u8, std.mem.sliceTo(enum_simple.owner_decl.name, 0));
+ return try ally.dupeZ(u8, std.mem.sliceTo(enum_simple.owner_decl.name, 0));
},
.enum_numbered => {
const enum_numbered = ty.castTag(.enum_numbered).?.data;
- return try arena.dupeZ(u8, std.mem.sliceTo(enum_numbered.owner_decl.name, 0));
+ return try ally.dupeZ(u8, std.mem.sliceTo(enum_numbered.owner_decl.name, 0));
},
.@"opaque" => {
- // TODO use declaration name
- return "opaque {}";
- },
-
- .anyerror_void_error_union => return "anyerror!void",
- .const_slice_u8 => return "[]const u8",
- .const_slice_u8_sentinel_0 => return "[:0]const u8",
- .fn_noreturn_no_args => return "fn() noreturn",
- .fn_void_no_args => return "fn() void",
- .fn_naked_noreturn_no_args => return "fn() callconv(.Naked) noreturn",
- .fn_ccc_void_no_args => return "fn() callconv(.C) void",
- .single_const_pointer_to_comptime_int => return "*const comptime_int",
- .manyptr_u8 => return "[*]u8",
- .manyptr_const_u8 => return "[*]const u8",
- .manyptr_const_u8_sentinel_0 => return "[*:0]const u8",
- .atomic_order => return "AtomicOrder",
- .atomic_rmw_op => return "AtomicRmwOp",
- .calling_convention => return "CallingConvention",
- .address_space => return "AddressSpace",
- .float_mode => return "FloatMode",
- .reduce_op => return "ReduceOp",
- .call_options => return "CallOptions",
- .prefetch_options => return "PrefetchOptions",
- .export_options => return "ExportOptions",
- .extern_options => return "ExternOptions",
- .type_info => return "Type",
+ const opaque_obj = ty.cast(Payload.Opaque).?.data;
+ return try ally.dupeZ(u8, std.mem.sliceTo(opaque_obj.owner_decl.name, 0));
+ },
+
+ .anyerror_void_error_union => return maybeDupe("anyerror!void", ally, is_arena),
+ .const_slice_u8 => return maybeDupe("[]const u8", ally, is_arena),
+ .const_slice_u8_sentinel_0 => return maybeDupe("[:0]const u8", ally, is_arena),
+ .fn_noreturn_no_args => return maybeDupe("fn() noreturn", ally, is_arena),
+ .fn_void_no_args => return maybeDupe("fn() void", ally, is_arena),
+ .fn_naked_noreturn_no_args => return maybeDupe("fn() callconv(.Naked) noreturn", ally, is_arena),
+ .fn_ccc_void_no_args => return maybeDupe("fn() callconv(.C) void", ally, is_arena),
+ .single_const_pointer_to_comptime_int => return maybeDupe("*const comptime_int", ally, is_arena),
+ .manyptr_u8 => return maybeDupe("[*]u8", ally, is_arena),
+ .manyptr_const_u8 => return maybeDupe("[*]const u8", ally, is_arena),
+ .manyptr_const_u8_sentinel_0 => return maybeDupe("[*:0]const u8", ally, is_arena),
+ .atomic_order => return maybeDupe("AtomicOrder", ally, is_arena),
+ .atomic_rmw_op => return maybeDupe("AtomicRmwOp", ally, is_arena),
+ .calling_convention => return maybeDupe("CallingConvention", ally, is_arena),
+ .address_space => return maybeDupe("AddressSpace", ally, is_arena),
+ .float_mode => return maybeDupe("FloatMode", ally, is_arena),
+ .reduce_op => return maybeDupe("ReduceOp", ally, is_arena),
+ .call_options => return maybeDupe("CallOptions", ally, is_arena),
+ .prefetch_options => return maybeDupe("PrefetchOptions", ally, is_arena),
+ .export_options => return maybeDupe("ExportOptions", ally, is_arena),
+ .extern_options => return maybeDupe("ExternOptions", ally, is_arena),
+ .type_info => return maybeDupe("Type", ally, is_arena),
else => {
// TODO this is wasteful and also an incorrect implementation of `@typeName`
- var buf = std.ArrayList(u8).init(arena);
+ var buf = std.ArrayList(u8).init(ally);
try buf.writer().print("{}", .{ty});
return try buf.toOwnedSliceSentinel(0);
},
}
}
+ fn maybeDupe(s: [:0]const u8, ally: Allocator, is_arena: bool) Allocator.Error![:0]const u8 {
+ if (is_arena) {
+ return s;
+ } else {
+ return try ally.dupeZ(u8, s);
+ }
+ }
+
pub fn toValue(self: Type, allocator: Allocator) Allocator.Error!Value {
switch (self.tag()) {
.u1 => return Value.initTag(.u1_type),