@@ -1766,8 +1766,20 @@ pub const Type = extern union {
1766
1766
}
1767
1767
}
1768
1768
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
+
1769
1777
/// 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 {
1771
1783
const t = ty .tag ();
1772
1784
switch (t ) {
1773
1785
.inferred_alloc_const = > unreachable ,
@@ -1812,71 +1824,79 @@ pub const Type = extern union {
1812
1824
.noreturn ,
1813
1825
.var_args_param ,
1814
1826
.bound_fn ,
1815
- = > return @tagName (t ),
1827
+ = > return maybeDupe ( @tagName (t ), ally , is_arena ),
1816
1828
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 ) ,
1820
1832
1821
- .empty_struct , .empty_struct_literal = > return "struct {}" ,
1833
+ .empty_struct , .empty_struct_literal = > return maybeDupe ( "struct {}" , ally , is_arena ) ,
1822
1834
1823
1835
.@"struct" = > {
1824
1836
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 ));
1826
1838
},
1827
1839
.@"union" , .union_tagged = > {
1828
1840
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 ));
1830
1842
},
1831
1843
.enum_full , .enum_nonexhaustive = > {
1832
1844
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 ));
1834
1846
},
1835
1847
.enum_simple = > {
1836
1848
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 ));
1838
1850
},
1839
1851
.enum_numbered = > {
1840
1852
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 ));
1842
1854
},
1843
1855
.@"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 ) ,
1870
1882
1871
1883
else = > {
1872
1884
// 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 );
1874
1886
try buf .writer ().print ("{}" , .{ty });
1875
1887
return try buf .toOwnedSliceSentinel (0 );
1876
1888
},
1877
1889
}
1878
1890
}
1879
1891
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
+
1880
1900
pub fn toValue (self : Type , allocator : Allocator ) Allocator.Error ! Value {
1881
1901
switch (self .tag ()) {
1882
1902
.u1 = > return Value .initTag (.u1_type ),
0 commit comments