@@ -1766,8 +1766,20 @@ pub const Type = extern union {
17661766 }
17671767 }
17681768
1769+ pub fn nameAllocArena (ty : Type , arena : Allocator ) Allocator.Error ! [:0 ]const u8 {
1770+ return nameAllocAdvanced (ty , arena , true );
1771+ }
1772+
1773+ pub fn nameAlloc (ty : Type , gpa : Allocator ) Allocator.Error ! [:0 ]const u8 {
1774+ return nameAllocAdvanced (ty , gpa , false );
1775+ }
1776+
17691777 /// Returns a name suitable for `@typeName`.
1770- pub fn nameAlloc (ty : Type , arena : Allocator ) Allocator.Error ! [:0 ]const u8 {
1778+ pub fn nameAllocAdvanced (
1779+ ty : Type ,
1780+ ally : Allocator ,
1781+ is_arena : bool ,
1782+ ) Allocator.Error ! [:0 ]const u8 {
17711783 const t = ty .tag ();
17721784 switch (t ) {
17731785 .inferred_alloc_const = > unreachable ,
@@ -1812,71 +1824,79 @@ pub const Type = extern union {
18121824 .noreturn ,
18131825 .var_args_param ,
18141826 .bound_fn ,
1815- = > return @tagName (t ),
1827+ = > return maybeDupe ( @tagName (t ), ally , is_arena ),
18161828
1817- .enum_literal = > return "@Type(.EnumLiteral)" ,
1818- .@"null" = > return "@Type(.Null)" ,
1819- .@"undefined" = > return "@Type(.Undefined)" ,
1829+ .enum_literal = > return maybeDupe ( "@Type(.EnumLiteral)" , ally , is_arena ) ,
1830+ .@"null" = > return maybeDupe ( "@Type(.Null)" , ally , is_arena ) ,
1831+ .@"undefined" = > return maybeDupe ( "@Type(.Undefined)" , ally , is_arena ) ,
18201832
1821- .empty_struct , .empty_struct_literal = > return "struct {}" ,
1833+ .empty_struct , .empty_struct_literal = > return maybeDupe ( "struct {}" , ally , is_arena ) ,
18221834
18231835 .@"struct" = > {
18241836 const struct_obj = ty .castTag (.@"struct" ).? .data ;
1825- return try arena .dupeZ (u8 , std .mem .sliceTo (struct_obj .owner_decl .name , 0 ));
1837+ return try ally .dupeZ (u8 , std .mem .sliceTo (struct_obj .owner_decl .name , 0 ));
18261838 },
18271839 .@"union" , .union_tagged = > {
18281840 const union_obj = ty .cast (Payload .Union ).? .data ;
1829- return try arena .dupeZ (u8 , std .mem .sliceTo (union_obj .owner_decl .name , 0 ));
1841+ return try ally .dupeZ (u8 , std .mem .sliceTo (union_obj .owner_decl .name , 0 ));
18301842 },
18311843 .enum_full , .enum_nonexhaustive = > {
18321844 const enum_full = ty .cast (Payload .EnumFull ).? .data ;
1833- return try arena .dupeZ (u8 , std .mem .sliceTo (enum_full .owner_decl .name , 0 ));
1845+ return try ally .dupeZ (u8 , std .mem .sliceTo (enum_full .owner_decl .name , 0 ));
18341846 },
18351847 .enum_simple = > {
18361848 const enum_simple = ty .castTag (.enum_simple ).? .data ;
1837- return try arena .dupeZ (u8 , std .mem .sliceTo (enum_simple .owner_decl .name , 0 ));
1849+ return try ally .dupeZ (u8 , std .mem .sliceTo (enum_simple .owner_decl .name , 0 ));
18381850 },
18391851 .enum_numbered = > {
18401852 const enum_numbered = ty .castTag (.enum_numbered ).? .data ;
1841- return try arena .dupeZ (u8 , std .mem .sliceTo (enum_numbered .owner_decl .name , 0 ));
1853+ return try ally .dupeZ (u8 , std .mem .sliceTo (enum_numbered .owner_decl .name , 0 ));
18421854 },
18431855 .@"opaque" = > {
1844- // TODO use declaration name
1845- return "opaque {}" ;
1846- },
1847-
1848- .anyerror_void_error_union = > return "anyerror!void" ,
1849- .const_slice_u8 = > return "[]const u8" ,
1850- .const_slice_u8_sentinel_0 = > return "[:0]const u8" ,
1851- .fn_noreturn_no_args = > return "fn() noreturn" ,
1852- .fn_void_no_args = > return "fn() void" ,
1853- .fn_naked_noreturn_no_args = > return "fn() callconv(.Naked) noreturn" ,
1854- .fn_ccc_void_no_args = > return "fn() callconv(.C) void" ,
1855- .single_const_pointer_to_comptime_int = > return "*const comptime_int" ,
1856- .manyptr_u8 = > return "[*]u8" ,
1857- .manyptr_const_u8 = > return "[*]const u8" ,
1858- .manyptr_const_u8_sentinel_0 = > return "[*:0]const u8" ,
1859- .atomic_order = > return "AtomicOrder" ,
1860- .atomic_rmw_op = > return "AtomicRmwOp" ,
1861- .calling_convention = > return "CallingConvention" ,
1862- .address_space = > return "AddressSpace" ,
1863- .float_mode = > return "FloatMode" ,
1864- .reduce_op = > return "ReduceOp" ,
1865- .call_options = > return "CallOptions" ,
1866- .prefetch_options = > return "PrefetchOptions" ,
1867- .export_options = > return "ExportOptions" ,
1868- .extern_options = > return "ExternOptions" ,
1869- .type_info = > return "Type" ,
1856+ const opaque_obj = ty . cast ( Payload . Opaque ) .? . data ;
1857+ return try ally . dupeZ ( u8 , std . mem . sliceTo ( opaque_obj . owner_decl . name , 0 )) ;
1858+ },
1859+
1860+ .anyerror_void_error_union = > return maybeDupe ( "anyerror!void" , ally , is_arena ) ,
1861+ .const_slice_u8 = > return maybeDupe ( "[]const u8" , ally , is_arena ) ,
1862+ .const_slice_u8_sentinel_0 = > return maybeDupe ( "[:0]const u8" , ally , is_arena ) ,
1863+ .fn_noreturn_no_args = > return maybeDupe ( "fn() noreturn" , ally , is_arena ) ,
1864+ .fn_void_no_args = > return maybeDupe ( "fn() void" , ally , is_arena ) ,
1865+ .fn_naked_noreturn_no_args = > return maybeDupe ( "fn() callconv(.Naked) noreturn" , ally , is_arena ) ,
1866+ .fn_ccc_void_no_args = > return maybeDupe ( "fn() callconv(.C) void" , ally , is_arena ) ,
1867+ .single_const_pointer_to_comptime_int = > return maybeDupe ( "*const comptime_int" , ally , is_arena ) ,
1868+ .manyptr_u8 = > return maybeDupe ( "[*]u8" , ally , is_arena ) ,
1869+ .manyptr_const_u8 = > return maybeDupe ( "[*]const u8" , ally , is_arena ) ,
1870+ .manyptr_const_u8_sentinel_0 = > return maybeDupe ( "[*:0]const u8" , ally , is_arena ) ,
1871+ .atomic_order = > return maybeDupe ( "AtomicOrder" , ally , is_arena ) ,
1872+ .atomic_rmw_op = > return maybeDupe ( "AtomicRmwOp" , ally , is_arena ) ,
1873+ .calling_convention = > return maybeDupe ( "CallingConvention" , ally , is_arena ) ,
1874+ .address_space = > return maybeDupe ( "AddressSpace" , ally , is_arena ) ,
1875+ .float_mode = > return maybeDupe ( "FloatMode" , ally , is_arena ) ,
1876+ .reduce_op = > return maybeDupe ( "ReduceOp" , ally , is_arena ) ,
1877+ .call_options = > return maybeDupe ( "CallOptions" , ally , is_arena ) ,
1878+ .prefetch_options = > return maybeDupe ( "PrefetchOptions" , ally , is_arena ) ,
1879+ .export_options = > return maybeDupe ( "ExportOptions" , ally , is_arena ) ,
1880+ .extern_options = > return maybeDupe ( "ExternOptions" , ally , is_arena ) ,
1881+ .type_info = > return maybeDupe ( "Type" , ally , is_arena ) ,
18701882
18711883 else = > {
18721884 // TODO this is wasteful and also an incorrect implementation of `@typeName`
1873- var buf = std .ArrayList (u8 ).init (arena );
1885+ var buf = std .ArrayList (u8 ).init (ally );
18741886 try buf .writer ().print ("{}" , .{ty });
18751887 return try buf .toOwnedSliceSentinel (0 );
18761888 },
18771889 }
18781890 }
18791891
1892+ fn maybeDupe (s : [:0 ]const u8 , ally : Allocator , is_arena : bool ) Allocator.Error ! [:0 ]const u8 {
1893+ if (is_arena ) {
1894+ return s ;
1895+ } else {
1896+ return try ally .dupeZ (u8 , s );
1897+ }
1898+ }
1899+
18801900 pub fn toValue (self : Type , allocator : Allocator ) Allocator.Error ! Value {
18811901 switch (self .tag ()) {
18821902 .u1 = > return Value .initTag (.u1_type ),
0 commit comments