authorgravatar for der.teufel.mail@gmail.comKrzysztof Wolicki <der.teufel.mail@gmail.com> 2026-04-30 14:48:37+02:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-05-27 09:19:51+01:00
log5ed8e9324e77d0c94d7f5ad80025aa22f4a2eea8
treece7a8285f29989668f8d701459c3a6cc074e32f7
parent284ab0ad86310df45ecc3887cb6ed2f8cf507e45
signaturelock-open Commit is signed but in an unrecognized format.

Sema: generate new struct-of-arrays style values in zirTypeInfo


2 files changed, 414 insertions(+), 176 deletions(-)

src/Sema.zig+405-157
......@@ -2822,6 +2822,17 @@ fn interpretStdLangType(
28222822 };
28232823}
28242824
2825fn uninterpretStdLangType(
2826 sema: *Sema,
2827 val: anytype,
2828 ty: Type,
2829) !Value {
2830 return Value.uninterpret(val, ty, sema.pt) catch |err| switch (err) {
2831 error.OutOfMemory => |e| return e,
2832 error.TypeMismatch => @panic("std.lang is corrupt"),
2833 };
2834}
2835
28252836fn zirTupleDecl(
28262837 sema: *Sema,
28272838 block: *Block,
......@@ -15988,13 +15999,19 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1598815999
1598916000 .@"fn" => {
1599016001 const fn_info_ty = try sema.getStdLangType(src, .@"Type.Fn");
15991 const param_info_ty = try sema.getStdLangType(src, .@"Type.Fn.Param");
16002 const param_attrs_ty = try sema.getStdLangType(src, .@"Type.Fn.ParamAttributes");
16003 const fn_attr_ty = try sema.getStdLangType(src, .@"Type.Fn.Attributes");
1599216004
1599316005 const func_ty_info = zcu.typeToFunc(ty).?;
15994 const param_vals = try sema.arena.alloc(InternPool.Index, func_ty_info.param_types.len);
16006 const param_type_vals = try sema.arena.alloc(InternPool.Index, func_ty_info.param_types.len);
16007 const param_attr_vals = try sema.arena.alloc(InternPool.Index, func_ty_info.param_types.len);
1599516008 var func_is_generic = false;
1599616009
15997 for (param_vals, 0..) |*param_val, param_index| {
16010 for (
16011 param_type_vals,
16012 param_attr_vals,
16013 0..,
16014 ) |*param_type_val, *param_attr_val, param_index| {
1599816015 const param_ty = func_ty_info.param_types.get(ip)[param_index];
1599916016 const is_generic = param_ty == .generic_poison_type;
1600016017 const is_noalias, const is_comptime = flags: {
......@@ -16011,25 +16028,23 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1601116028 .val = if (is_generic) .none else param_ty,
1601216029 } });
1601316030
16014 const param_fields = .{
16015 // is_generic: bool,
16016 Value.makeBool(is_generic).toIntern(),
16017 // is_noalias: bool,
16031 const param_attrs_fields = .{
16032 // @"noalias": bool,
1601816033 Value.makeBool(is_noalias).toIntern(),
16019 // type: ?type,
16020 param_ty_val,
1602116034 };
16022 param_val.* = (try pt.aggregateValue(param_info_ty, &param_fields)).toIntern();
16035
16036 param_type_val.* = param_ty_val;
16037 param_attr_val.* = (try pt.aggregateValue(param_attrs_ty, &param_attrs_fields)).toIntern();
1602316038 }
1602416039
16025 const args_val = v: {
16040 const param_types_val = v: {
1602616041 const new_decl_ty = try pt.arrayType(.{
16027 .len = param_vals.len,
16028 .child = param_info_ty.toIntern(),
16042 .len = param_type_vals.len,
16043 .child = try pt.intern(.{ .opt_type = .type_type }),
1602916044 });
16030 const new_decl_val = (try pt.aggregateValue(new_decl_ty, param_vals)).toIntern();
16045 const new_decl_val = (try pt.aggregateValue(new_decl_ty, param_type_vals)).toIntern();
1603116046 const slice_ty = (try pt.ptrType(.{
16032 .child = param_info_ty.toIntern(),
16047 .child = try pt.intern(.{ .opt_type = .type_type }),
1603316048 .flags = .{
1603416049 .size = .slice,
1603516050 .is_const = true,
......@@ -16046,7 +16061,34 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1604616061 } },
1604716062 .byte_offset = 0,
1604816063 } }),
16049 .len = (try pt.intValue(.usize, param_vals.len)).toIntern(),
16064 .len = (try pt.intValue(.usize, param_type_vals.len)).toIntern(),
16065 } });
16066 };
16067 const param_attrs_val = v: {
16068 const new_decl_ty = try pt.arrayType(.{
16069 .len = param_attr_vals.len,
16070 .child = param_attrs_ty.toIntern(),
16071 });
16072 const new_decl_val = (try pt.aggregateValue(new_decl_ty, param_attr_vals)).toIntern();
16073 const slice_ty = (try pt.ptrType(.{
16074 .child = param_attrs_ty.toIntern(),
16075 .flags = .{
16076 .size = .slice,
16077 .is_const = true,
16078 },
16079 })).toIntern();
16080 const manyptr_ty = Type.fromInterned(slice_ty).slicePtrFieldType(zcu).toIntern();
16081 break :v try pt.intern(.{ .slice = .{
16082 .ty = slice_ty,
16083 .ptr = try pt.intern(.{ .ptr = .{
16084 .ty = manyptr_ty,
16085 .base_addr = .{ .uav = .{
16086 .orig_ty = manyptr_ty,
16087 .val = new_decl_val,
16088 } },
16089 .byte_offset = 0,
16090 } }),
16091 .len = (try pt.intValue(.usize, param_attr_vals.len)).toIntern(),
1605016092 } });
1605116093 };
1605216094
......@@ -16075,17 +16117,25 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1607516117 error.OutOfMemory => |e| return e,
1607616118 };
1607716119
16078 const field_values: [5]InternPool.Index = .{
16079 // calling_convention: CallingConvention,
16120 const fn_attrs_values = .{
16121 // @"callconv": CallingConvention = .auto,
1608016122 callconv_val.toIntern(),
16123 // varargs: bool = false,
16124 Value.makeBool(func_ty_info.is_var_args).toIntern(),
16125 };
16126
16127 const field_values = .{
16128 // attrs: Attributes,
16129 (try pt.aggregateValue(fn_attr_ty, &fn_attrs_values)).toIntern(),
1608116130 // is_generic: bool,
1608216131 Value.makeBool(func_is_generic).toIntern(),
16083 // is_var_args: bool,
16084 Value.makeBool(func_ty_info.is_var_args).toIntern(),
1608516132 // return_type: ?type,
1608616133 ret_ty_opt,
16087 // args: []const Fn.Param,
16088 args_val,
16134
16135 // param_types: []const ?type,
16136 param_types_val,
16137 // param_attrs: []const ParamAttributes,
16138 param_attrs_val,
1608916139 };
1609016140 return Air.internedToRef((try pt.internUnion(.{
1609116141 .ty = type_info_ty.toIntern(),
......@@ -16139,23 +16189,34 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1613916189 const addrspace_ty = try sema.getStdLangType(src, .AddressSpace);
1614016190 const pointer_ty = try sema.getStdLangType(src, .@"Type.Pointer");
1614116191 const ptr_size_ty = try sema.getStdLangType(src, .@"Type.Pointer.Size");
16192 const ptr_attrs_ty = try sema.getStdLangType(src, .@"Type.Pointer.Attributes");
1614216193
16143 const field_values = .{
16144 // size: Size,
16145 (try pt.enumValueFieldIndex(ptr_size_ty, @intFromEnum(info.flags.size))).toIntern(),
16146 // is_const: bool,
16194 const opt_addrspace_val = try pt.intern(.{ .opt = .{
16195 .ty = (try pt.optionalType(addrspace_ty.toIntern())).toIntern(),
16196 .val = (try sema.uninterpretStdLangType(info.flags.address_space, addrspace_ty)).toIntern(),
16197 } });
16198
16199 const attributes = .{
16200 // @"const": bool = false,
1614716201 Value.makeBool(info.flags.is_const).toIntern(),
16148 // is_volatile: bool,
16202 // @"volatile": bool = false,
1614916203 Value.makeBool(info.flags.is_volatile).toIntern(),
16150 // alignment: ?usize,
16204 // @"allowzero": bool = false,
16205 Value.makeBool(info.flags.is_allowzero).toIntern(),
16206 // @"addrspace": ?AddressSpace = null,
16207 opt_addrspace_val,
16208 // @"align": ?usize = null,
1615116209 alignment_val.toIntern(),
16152 // address_space: AddressSpace
16153 (try pt.enumValueFieldIndex(addrspace_ty, @intFromEnum(info.flags.address_space))).toIntern(),
16210 };
16211
16212 const field_values = .{
16213 // size: Size,
16214 (try pt.enumValueFieldIndex(ptr_size_ty, @intFromEnum(info.flags.size))).toIntern(),
16215 // attrs: Attributes
16216 (try pt.aggregateValue(ptr_attrs_ty, &attributes)).toIntern(),
1615416217 // child: type,
1615516218 info.child,
16156 // is_allowzero: bool,
16157 Value.makeBool(info.flags.is_allowzero).toIntern(),
16158 // sentinel: ?*const anyopaque,
16219 // sentinel_ptr: ?*const anyopaque,
1615916220 (try sema.optRefValue(switch (info.sentinel) {
1616016221 .none => null,
1616116222 else => Value.fromInterned(info.sentinel),
......@@ -16215,8 +16276,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1621516276 })));
1621616277 },
1621716278 .error_set => {
16218 // Get the Error type
16219 const error_field_ty = try sema.getStdLangType(src, .@"Type.Error");
16279 const error_set_ty = try sema.getStdLangType(src, .@"Type.ErrorSet");
1622016280
1622116281 // Build our list of Error values
1622216282 // Optional value is only null if anyerror
......@@ -16253,20 +16313,16 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1625316313 } });
1625416314 };
1625516315
16256 const error_field_fields = .{
16257 // name: [:0]const u8,
16258 error_name_val,
16259 };
16260 field_val.* = (try pt.aggregateValue(error_field_ty, &error_field_fields)).toIntern();
16316 field_val.* = error_name_val;
1626116317 }
1626216318
1626316319 break :blk vals;
1626416320 },
1626516321 };
1626616322
16267 // Build our ?[]const Error value
16323 // Build our ?[]const [:0]const u8 value
1626816324 const slice_errors_ty = try pt.ptrType(.{
16269 .child = error_field_ty.toIntern(),
16325 .child = .slice_const_u8_sentinel_0_type,
1627016326 .flags = .{
1627116327 .size = .slice,
1627216328 .is_const = true,
......@@ -16276,7 +16332,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1627616332 const errors_payload_val: InternPool.Index = if (error_field_vals) |vals| v: {
1627716333 const array_errors_ty = try pt.arrayType(.{
1627816334 .len = vals.len,
16279 .child = error_field_ty.toIntern(),
16335 .child = .slice_const_u8_sentinel_0_type,
1628016336 });
1628116337 const new_decl_val = (try pt.aggregateValue(array_errors_ty, vals)).toIntern();
1628216338 const manyptr_errors_ty = slice_errors_ty.slicePtrFieldType(zcu).toIntern();
......@@ -16298,11 +16354,16 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1629816354 .val = errors_payload_val,
1629916355 } });
1630016356
16357 const field_values = .{
16358 // error_names: ?[]const [:0]const u8
16359 errors_val,
16360 };
16361
1630116362 // Construct Type{ .error_set = errors_val }
1630216363 return Air.internedToRef((try pt.internUnion(.{
1630316364 .ty = type_info_ty.toIntern(),
1630416365 .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.lang.TypeId.error_set))).toIntern(),
16305 .val = errors_val,
16366 .val = (try pt.aggregateValue(error_set_ty, &field_values)).toIntern(),
1630616367 })));
1630716368 },
1630816369 .error_union => {
......@@ -16322,12 +16383,20 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1632216383 },
1632316384 .@"enum" => {
1632416385 const enum_obj = ip.loadEnumType(ty.toIntern());
16325 const is_exhaustive: Value = .makeBool(!enum_obj.nonexhaustive);
1632616386
16327 const enum_field_ty = try sema.getStdLangType(src, .@"Type.EnumField");
16387 const enum_mode_ty = try sema.getStdLangType(src, .@"Type.Enum.Mode");
1632816388
16329 const enum_field_vals = try sema.arena.alloc(InternPool.Index, enum_obj.field_names.len);
16330 for (enum_field_vals, 0..) |*field_val, tag_index| {
16389 const enum_mode_tag: std.builtin.Type.Enum.Mode = if (enum_obj.nonexhaustive) .nonexhaustive else .exhaustive;
16390
16391 const enum_mode: Value = try sema.uninterpretStdLangType(enum_mode_tag, enum_mode_ty);
16392
16393 const enum_field_name_vals = try sema.arena.alloc(InternPool.Index, enum_obj.field_names.len);
16394 const enum_field_value_vals = try sema.arena.alloc(InternPool.Index, enum_obj.field_names.len);
16395 for (
16396 enum_field_name_vals,
16397 enum_field_value_vals,
16398 0..,
16399 ) |*field_name_val, *field_value_val, tag_index| {
1633116400 const value_val = if (enum_obj.field_values.len > 0)
1633216401 try ip.getCoercedInts(
1633316402 gpa,
......@@ -16366,23 +16435,18 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1636616435 } });
1636716436 };
1636816437
16369 const enum_field_fields = .{
16370 // name: [:0]const u8,
16371 name_val,
16372 // value: comptime_int,
16373 value_val,
16374 };
16375 field_val.* = (try pt.aggregateValue(enum_field_ty, &enum_field_fields)).toIntern();
16438 field_name_val.* = name_val;
16439 field_value_val.* = value_val;
1637616440 }
1637716441
16378 const fields_val = v: {
16379 const fields_array_ty = try pt.arrayType(.{
16380 .len = enum_field_vals.len,
16381 .child = enum_field_ty.toIntern(),
16442 const fields_names_val = v: {
16443 const fields_names_array_ty = try pt.arrayType(.{
16444 .len = enum_field_name_vals.len,
16445 .child = .slice_const_u8_sentinel_0_type,
1638216446 });
16383 const new_decl_val = (try pt.aggregateValue(fields_array_ty, enum_field_vals)).toIntern();
16447 const new_decl_val = (try pt.aggregateValue(fields_names_array_ty, enum_field_name_vals)).toIntern();
1638416448 const slice_ty = (try pt.ptrType(.{
16385 .child = enum_field_ty.toIntern(),
16449 .child = .slice_const_u8_sentinel_0_type,
1638616450 .flags = .{
1638716451 .size = .slice,
1638816452 .is_const = true,
......@@ -16399,23 +16463,55 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1639916463 } },
1640016464 .byte_offset = 0,
1640116465 } }),
16402 .len = (try pt.intValue(.usize, enum_field_vals.len)).toIntern(),
16466 .len = (try pt.intValue(.usize, enum_field_name_vals.len)).toIntern(),
1640316467 } });
1640416468 };
1640516469
16406 const decls_val = try sema.typeInfoDecls(src, ip.loadEnumType(ty.toIntern()).namespace.toOptional());
16470 const fields_values_val = v: {
16471 const fields_values_array_ty = try pt.arrayType(.{
16472 .len = enum_field_value_vals.len,
16473 .child = .comptime_int_type,
16474 });
16475 const new_decl_val = (try pt.aggregateValue(fields_values_array_ty, enum_field_value_vals)).toIntern();
16476 const slice_ty = (try pt.ptrType(.{
16477 .child = .comptime_int_type,
16478 .flags = .{
16479 .size = .slice,
16480 .is_const = true,
16481 },
16482 })).toIntern();
16483 const manyptr_ty = Type.fromInterned(slice_ty).slicePtrFieldType(zcu).toIntern();
16484 break :v try pt.intern(.{ .slice = .{
16485 .ty = slice_ty,
16486 .ptr = try pt.intern(.{ .ptr = .{
16487 .ty = manyptr_ty,
16488 .base_addr = .{ .uav = .{
16489 .val = new_decl_val,
16490 .orig_ty = manyptr_ty,
16491 } },
16492 .byte_offset = 0,
16493 } }),
16494 .len = (try pt.intValue(.usize, enum_field_value_vals.len)).toIntern(),
16495 } });
16496 };
16497
16498 const decl_names_val = try sema.typeInfoDecls(ip.loadEnumType(ty.toIntern()).namespace.toOptional());
1640716499
1640816500 const type_enum_ty = try sema.getStdLangType(src, .@"Type.Enum");
1640916501
1641016502 const field_values = .{
1641116503 // tag_type: type,
1641216504 ip.loadEnumType(ty.toIntern()).int_tag_type,
16413 // fields: []const EnumField,
16414 fields_val,
16415 // decls: []const Declaration,
16416 decls_val,
16417 // is_exhaustive: bool,
16418 is_exhaustive.toIntern(),
16505 // mode: Mode
16506 enum_mode.toIntern(),
16507
16508 // field_names: []const [:0]const u8,
16509 fields_names_val,
16510 // field_values: []const comptime_int,
16511 fields_values_val,
16512
16513 // decl_names: []const [:0]const u8,
16514 decl_names_val,
1641916515 };
1642016516 return Air.internedToRef((try pt.internUnion(.{
1642116517 .ty = type_info_ty.toIntern(),
......@@ -16425,16 +16521,26 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1642516521 },
1642616522 .@"union" => {
1642716523 const type_union_ty = try sema.getStdLangType(src, .@"Type.Union");
16428 const union_field_ty = try sema.getStdLangType(src, .@"Type.UnionField");
16524 const union_field_attr_ty = try sema.getStdLangType(src, .@"Type.Union.FieldAttributes");
1642916525
1643016526 const union_obj = ip.loadUnionType(ty.toIntern());
1643116527 const enum_obj = ip.loadEnumType(union_obj.enum_tag_type);
1643216528 const layout = union_obj.layout;
1643316529
16434 const union_field_vals = try gpa.alloc(InternPool.Index, enum_obj.field_names.len);
16435 defer gpa.free(union_field_vals);
16436
16437 for (union_field_vals, 0..) |*field_val, field_index| {
16530 const union_field_names = try gpa.alloc(InternPool.Index, enum_obj.field_names.len);
16531 defer gpa.free(union_field_names);
16532 const union_field_attrs = try gpa.alloc(InternPool.Index, enum_obj.field_names.len);
16533 defer gpa.free(union_field_attrs);
16534
16535 for (
16536 union_field_names,
16537 union_field_attrs,
16538 0..,
16539 ) |
16540 *field_name_val,
16541 *field_attr_val,
16542 field_index,
16543 | {
1643816544 const name_val = v: {
1643916545 const field_name = enum_obj.field_names.get(ip)[field_index];
1644016546 const field_name_len = field_name.length(ip);
......@@ -16461,8 +16567,6 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1646116567 } });
1646216568 };
1646316569
16464 const field_ty: Type = .fromInterned(union_obj.field_types.get(ip)[field_index]);
16465
1646616570 const alignment_ty = try pt.optionalType(.usize_type);
1646716571 const alignment_val: Value = val: {
1646816572 const a: Alignment = switch (layout) {
......@@ -16479,25 +16583,52 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1647916583 } }));
1648016584 };
1648116585
16482 const union_field_fields = .{
16483 // name: [:0]const u8,
16484 name_val,
16485 // type: type,
16486 field_ty.toIntern(),
16586 field_name_val.* = name_val;
16587 const union_field_attr = .{
1648716588 // alignment: ?usize,
1648816589 alignment_val.toIntern(),
1648916590 };
16490 field_val.* = (try pt.aggregateValue(union_field_ty, &union_field_fields)).toIntern();
16591
16592 field_attr_val.* = (try pt.aggregateValue(union_field_attr_ty, &union_field_attr)).toIntern();
1649116593 }
1649216594
16493 const fields_val = v: {
16595 const field_names_val = v: {
16596 const array_field_names_ty = try pt.arrayType(.{
16597 .len = union_field_names.len,
16598 .child = .slice_const_u8_sentinel_0_type,
16599 });
16600 const new_decl_val = (try pt.aggregateValue(array_field_names_ty, union_field_names)).toIntern();
16601 const slice_ty = (try pt.ptrType(.{
16602 .child = .slice_const_u8_sentinel_0_type,
16603 .flags = .{
16604 .size = .slice,
16605 .is_const = true,
16606 },
16607 })).toIntern();
16608 const manyptr_ty = Type.fromInterned(slice_ty).slicePtrFieldType(zcu).toIntern();
16609 break :v try pt.intern(.{ .slice = .{
16610 .ty = slice_ty,
16611 .ptr = try pt.intern(.{ .ptr = .{
16612 .ty = manyptr_ty,
16613 .base_addr = .{ .uav = .{
16614 .orig_ty = manyptr_ty,
16615 .val = new_decl_val,
16616 } },
16617 .byte_offset = 0,
16618 } }),
16619 .len = (try pt.intValue(.usize, union_field_names.len)).toIntern(),
16620 } });
16621 };
16622 const field_types_val = v: {
16623 const union_field_types = union_obj.field_types.get(ip);
16624
1649416625 const array_fields_ty = try pt.arrayType(.{
16495 .len = union_field_vals.len,
16496 .child = union_field_ty.toIntern(),
16626 .len = union_field_types.len,
16627 .child = .type_type,
1649716628 });
16498 const new_decl_val = (try pt.aggregateValue(array_fields_ty, union_field_vals)).toIntern();
16629 const new_decl_val = (try pt.aggregateValue(array_fields_ty, union_field_types)).toIntern();
1649916630 const slice_ty = (try pt.ptrType(.{
16500 .child = union_field_ty.toIntern(),
16631 .child = .type_type,
1650116632 .flags = .{
1650216633 .size = .slice,
1650316634 .is_const = true,
......@@ -16514,11 +16645,38 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1651416645 } },
1651516646 .byte_offset = 0,
1651616647 } }),
16517 .len = (try pt.intValue(.usize, union_field_vals.len)).toIntern(),
16648 .len = (try pt.intValue(.usize, union_field_types.len)).toIntern(),
16649 } });
16650 };
16651 const field_attrs_val = v: {
16652 const array_fields_ty = try pt.arrayType(.{
16653 .len = union_field_attrs.len,
16654 .child = union_field_attr_ty.toIntern(),
16655 });
16656 const new_decl_val = (try pt.aggregateValue(array_fields_ty, union_field_attrs)).toIntern();
16657 const slice_ty = (try pt.ptrType(.{
16658 .child = union_field_attr_ty.toIntern(),
16659 .flags = .{
16660 .size = .slice,
16661 .is_const = true,
16662 },
16663 })).toIntern();
16664 const manyptr_ty = Type.fromInterned(slice_ty).slicePtrFieldType(zcu).toIntern();
16665 break :v try pt.intern(.{ .slice = .{
16666 .ty = slice_ty,
16667 .ptr = try pt.intern(.{ .ptr = .{
16668 .ty = manyptr_ty,
16669 .base_addr = .{ .uav = .{
16670 .orig_ty = manyptr_ty,
16671 .val = new_decl_val,
16672 } },
16673 .byte_offset = 0,
16674 } }),
16675 .len = (try pt.intValue(.usize, union_field_attrs.len)).toIntern(),
1651816676 } });
1651916677 };
1652016678
16521 const decls_val = try sema.typeInfoDecls(src, ty.getNamespaceIndex(zcu).toOptional());
16679 const decl_names_val = try sema.typeInfoDecls(ty.getNamespaceIndex(zcu).toOptional());
1652216680
1652316681 const enum_tag_ty_val = try pt.intern(.{ .opt = .{
1652416682 .ty = (try pt.optionalType(.type_type)).toIntern(),
......@@ -16527,16 +16685,32 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1652716685
1652816686 const container_layout_ty = try sema.getStdLangType(src, .@"Type.ContainerLayout");
1652916687
16688 const backing_integer_val = try pt.intern(.{ .opt = .{
16689 .ty = (try pt.optionalType(.type_type)).toIntern(),
16690 .val = if (layout == .@"packed") val: {
16691 assert(Type.fromInterned(union_obj.packed_backing_int_type).isInt(zcu));
16692 break :val union_obj.packed_backing_int_type;
16693 } else .none,
16694 } });
16695
1653016696 const field_values = .{
1653116697 // layout: ContainerLayout,
1653216698 (try pt.enumValueFieldIndex(container_layout_ty, @intFromEnum(layout))).toIntern(),
1653316699
1653416700 // tag_type: ?type,
1653516701 enum_tag_ty_val,
16536 // fields: []const UnionField,
16537 fields_val,
16538 // decls: []const Declaration,
16539 decls_val,
16702 // backing_integer: ?type,
16703 backing_integer_val,
16704
16705 // field_names: []const [:0]const u8,
16706 field_names_val,
16707 // field_types: []const type,
16708 field_types_val,
16709 // field_attrs: []const FieldAttributes,
16710 field_attrs_val,
16711
16712 // decl_names: []const [:0]const u8,
16713 decl_names_val,
1654016714 };
1654116715 return Air.internedToRef((try pt.internUnion(.{
1654216716 .ty = type_info_ty.toIntern(),
......@@ -16546,16 +16720,26 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1654616720 },
1654716721 .@"struct" => {
1654816722 const type_struct_ty = try sema.getStdLangType(src, .@"Type.Struct");
16549 const struct_field_ty = try sema.getStdLangType(src, .@"Type.StructField");
16723 const struct_field_attr_ty = try sema.getStdLangType(src, .@"Type.Struct.FieldAttributes");
1655016724
16551 var struct_field_vals: []InternPool.Index = &.{};
16552 defer gpa.free(struct_field_vals);
16725 var struct_field_name_vals: []InternPool.Index = &.{};
16726 defer gpa.free(struct_field_name_vals);
16727 var struct_field_attr_vals: []InternPool.Index = &.{};
16728 defer gpa.free(struct_field_attr_vals);
1655316729 fv: {
1655416730 const struct_type = switch (ip.indexToKey(ty.toIntern())) {
1655516731 .tuple_type => |tuple_type| {
16556 struct_field_vals = try gpa.alloc(InternPool.Index, tuple_type.types.len);
16557 for (struct_field_vals, 0..) |*struct_field_val, field_index| {
16558 const field_ty = tuple_type.types.get(ip)[field_index];
16732 struct_field_name_vals = try gpa.alloc(InternPool.Index, tuple_type.types.len);
16733 struct_field_attr_vals = try gpa.alloc(InternPool.Index, tuple_type.types.len);
16734 for (
16735 struct_field_name_vals,
16736 struct_field_attr_vals,
16737 0..,
16738 ) |
16739 *struct_field_name_val,
16740 *struct_field_attr_val,
16741 field_index,
16742 | {
1655916743 const field_val = tuple_type.values.get(ip)[field_index];
1656016744 const name_val = v: {
1656116745 const field_name = try ip.getOrPutStringFmt(gpa, io, pt.tid, "{d}", .{field_index}, .no_embedded_nulls);
......@@ -16587,19 +16771,17 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1658716771 const opt_default_val = if (is_comptime) Value.fromInterned(field_val) else null;
1658816772 const default_val_ptr = try sema.optRefValue(opt_default_val);
1658916773
16590 const struct_field_fields = .{
16591 // name: [:0]const u8,
16592 name_val,
16593 // type: type,
16594 field_ty,
16595 // default_value: ?*const anyopaque,
16596 default_val_ptr.toIntern(),
16597 // is_comptime: bool,
16774 const struct_field_attr_fields = .{
16775 // @"comptime": bool,
1659816776 Value.makeBool(is_comptime).toIntern(),
16599 // alignment: ?usize,
16777 // @"align": ?usize,
1660016778 (try pt.nullValue(try pt.optionalType(.usize_type))).toIntern(),
16779 // default_value_ptr: ?*const anyopaque,
16780 default_val_ptr.toIntern(),
1660116781 };
16602 struct_field_val.* = (try pt.aggregateValue(struct_field_ty, &struct_field_fields)).toIntern();
16782
16783 struct_field_name_val.* = name_val;
16784 struct_field_attr_val.* = (try pt.aggregateValue(struct_field_attr_ty, &struct_field_attr_fields)).toIntern();
1660316785 }
1660416786 break :fv;
1660516787 },
......@@ -16607,12 +16789,20 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1660716789 else => unreachable,
1660816790 };
1660916791 try sema.ensureStructDefaultsResolved(ty, src); // can't do this sooner, since it's not allowed on tuples
16610 struct_field_vals = try gpa.alloc(InternPool.Index, struct_type.field_types.len);
16611
16612 for (struct_field_vals, 0..) |*field_val, field_index| {
16792 struct_field_name_vals = try gpa.alloc(InternPool.Index, struct_type.field_types.len);
16793 struct_field_attr_vals = try gpa.alloc(InternPool.Index, struct_type.field_types.len);
16794
16795 for (
16796 struct_field_name_vals,
16797 struct_field_attr_vals,
16798 0..,
16799 ) |
16800 *field_name_val,
16801 *field_attr_val,
16802 field_index,
16803 | {
1661316804 const field_name = struct_type.field_names.get(ip)[field_index];
1661416805 const field_name_len = field_name.length(ip);
16615 const field_ty: Type = .fromInterned(struct_type.field_types.get(ip)[field_index]);
1661616806 const field_default: InternPool.Index = if (struct_type.field_defaults.len > 0) d: {
1661716807 break :d struct_type.field_defaults.get(ip)[field_index];
1661816808 } else .none;
......@@ -16660,30 +16850,63 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1666016850 } }));
1666116851 };
1666216852
16663 const struct_field_fields = .{
16664 // name: [:0]const u8,
16665 name_val,
16666 // type: type,
16667 field_ty.toIntern(),
16668 // default_value: ?*const anyopaque,
16669 default_val_ptr.toIntern(),
16670 // is_comptime: bool,
16853 const struct_field_attr_fields = .{
16854 // @"comptime": bool,
1667116855 Value.makeBool(field_is_comptime).toIntern(),
16672 // alignment: ?usize,
16856 // @"align": ?usize,
1667316857 alignment_val.toIntern(),
16858 // default_value_ptr: ?*const anyopaque,
16859 default_val_ptr.toIntern(),
1667416860 };
16675 field_val.* = (try pt.aggregateValue(struct_field_ty, &struct_field_fields)).toIntern();
16861 field_name_val.* = name_val;
16862 field_attr_val.* = (try pt.aggregateValue(struct_field_attr_ty, &struct_field_attr_fields)).toIntern();
1667616863 }
1667716864 }
1667816865
16679 const fields_val = v: {
16866 const field_names_val = v: {
16867 const array_fields_ty = try pt.arrayType(.{
16868 .len = struct_field_name_vals.len,
16869 .child = .slice_const_u8_sentinel_0_type,
16870 });
16871 const new_decl_val = (try pt.aggregateValue(array_fields_ty, struct_field_name_vals)).toIntern();
16872 const slice_ty = (try pt.ptrType(.{
16873 .child = .slice_const_u8_sentinel_0_type,
16874 .flags = .{
16875 .size = .slice,
16876 .is_const = true,
16877 },
16878 })).toIntern();
16879 const manyptr_ty = Type.fromInterned(slice_ty).slicePtrFieldType(zcu).toIntern();
16880 break :v try pt.intern(.{ .slice = .{
16881 .ty = slice_ty,
16882 .ptr = try pt.intern(.{ .ptr = .{
16883 .ty = manyptr_ty,
16884 .base_addr = .{ .uav = .{
16885 .orig_ty = manyptr_ty,
16886 .val = new_decl_val,
16887 } },
16888 .byte_offset = 0,
16889 } }),
16890 .len = (try pt.intValue(.usize, struct_field_name_vals.len)).toIntern(),
16891 } });
16892 };
16893
16894 const field_types_val = v: {
16895 const struct_field_type_vals = switch (ip.indexToKey(ty.toIntern())) {
16896 .tuple_type => |tt| tt.types.get(ip),
16897 .struct_type => blk: {
16898 const st = ip.loadStructType(ty.toIntern());
16899 break :blk st.field_types.get(ip);
16900 },
16901 else => unreachable,
16902 };
1668016903 const array_fields_ty = try pt.arrayType(.{
16681 .len = struct_field_vals.len,
16682 .child = struct_field_ty.toIntern(),
16904 .len = struct_field_type_vals.len,
16905 .child = .type_type,
1668316906 });
16684 const new_decl_val = (try pt.aggregateValue(array_fields_ty, struct_field_vals)).toIntern();
16907 const new_decl_val = (try pt.aggregateValue(array_fields_ty, struct_field_type_vals)).toIntern();
1668516908 const slice_ty = (try pt.ptrType(.{
16686 .child = struct_field_ty.toIntern(),
16909 .child = .type_type,
1668716910 .flags = .{
1668816911 .size = .slice,
1668916912 .is_const = true,
......@@ -16700,11 +16923,38 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1670016923 } },
1670116924 .byte_offset = 0,
1670216925 } }),
16703 .len = (try pt.intValue(.usize, struct_field_vals.len)).toIntern(),
16926 .len = (try pt.intValue(.usize, struct_field_type_vals.len)).toIntern(),
16927 } });
16928 };
16929 const field_attrs_val = v: {
16930 const array_fields_ty = try pt.arrayType(.{
16931 .len = struct_field_attr_vals.len,
16932 .child = struct_field_attr_ty.toIntern(),
16933 });
16934 const new_decl_val = (try pt.aggregateValue(array_fields_ty, struct_field_attr_vals)).toIntern();
16935 const slice_ty = (try pt.ptrType(.{
16936 .child = struct_field_attr_ty.toIntern(),
16937 .flags = .{
16938 .size = .slice,
16939 .is_const = true,
16940 },
16941 })).toIntern();
16942 const manyptr_ty = Type.fromInterned(slice_ty).slicePtrFieldType(zcu).toIntern();
16943 break :v try pt.intern(.{ .slice = .{
16944 .ty = slice_ty,
16945 .ptr = try pt.intern(.{ .ptr = .{
16946 .ty = manyptr_ty,
16947 .base_addr = .{ .uav = .{
16948 .orig_ty = manyptr_ty,
16949 .val = new_decl_val,
16950 } },
16951 .byte_offset = 0,
16952 } }),
16953 .len = (try pt.intValue(.usize, struct_field_attr_vals.len)).toIntern(),
1670416954 } });
1670516955 };
1670616956
16707 const decls_val = try sema.typeInfoDecls(src, ty.getNamespace(zcu));
16957 const decl_names_val = try sema.typeInfoDecls(ty.getNamespace(zcu));
1670816958
1670916959 const backing_integer_val = try pt.intern(.{ .opt = .{
1671016960 .ty = (try pt.optionalType(.type_type)).toIntern(),
......@@ -16719,16 +16969,22 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1671916969 const layout = ty.containerLayout(zcu);
1672016970
1672116971 const field_values = [_]InternPool.Index{
16972 // is_tuple: bool,
16973 Value.makeBool(ty.isTuple(zcu)).toIntern(),
1672216974 // layout: ContainerLayout,
1672316975 (try pt.enumValueFieldIndex(container_layout_ty, @intFromEnum(layout))).toIntern(),
1672416976 // backing_integer: ?type,
1672516977 backing_integer_val,
16726 // fields: []const StructField,
16727 fields_val,
16728 // decls: []const Declaration,
16729 decls_val,
16730 // is_tuple: bool,
16731 Value.makeBool(ty.isTuple(zcu)).toIntern(),
16978
16979 // field_names: []const [:0]const u8,
16980 field_names_val,
16981 // field_types: []const type,
16982 field_types_val,
16983 // field_attrs: []const FieldAttributes,
16984 field_attrs_val,
16985
16986 // decl_names: []const [:0]const u8,
16987 decl_names_val,
1673216988 };
1673316989 return Air.internedToRef((try pt.internUnion(.{
1673416990 .ty = type_info_ty.toIntern(),
......@@ -16739,11 +16995,11 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1673916995 .@"opaque" => {
1674016996 const type_opaque_ty = try sema.getStdLangType(src, .@"Type.Opaque");
1674116997
16742 const decls_val = try sema.typeInfoDecls(src, ty.getNamespace(zcu));
16998 const decl_names_val = try sema.typeInfoDecls(ty.getNamespace(zcu));
1674316999
1674417000 const field_values = .{
16745 // decls: []const Declaration,
16746 decls_val,
17001 // decl_names: []const [:0]const u8,
17002 decl_names_val,
1674717003 };
1674817004 return Air.internedToRef((try pt.internUnion(.{
1674917005 .ty = type_info_ty.toIntern(),
......@@ -16758,30 +17014,27 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1675817014
1675917015fn typeInfoDecls(
1676017016 sema: *Sema,
16761 src: LazySrcLoc,
1676217017 opt_namespace: InternPool.OptionalNamespaceIndex,
1676317018) CompileError!InternPool.Index {
1676417019 const pt = sema.pt;
1676517020 const zcu = pt.zcu;
1676617021 const gpa = sema.gpa;
1676717022
16768 const declaration_ty = try sema.getStdLangType(src, .@"Type.Declaration");
16769
1677017023 var decl_vals = std.array_list.Managed(InternPool.Index).init(gpa);
1677117024 defer decl_vals.deinit();
1677217025
1677317026 var seen_namespaces = std.AutoHashMap(*Namespace, void).init(gpa);
1677417027 defer seen_namespaces.deinit();
1677517028
16776 try sema.typeInfoNamespaceDecls(opt_namespace, declaration_ty, &decl_vals, &seen_namespaces);
17029 try sema.typeInfoNamespaceDecls(opt_namespace, &decl_vals, &seen_namespaces);
1677717030
1677817031 const array_decl_ty = try pt.arrayType(.{
1677917032 .len = decl_vals.items.len,
16780 .child = declaration_ty.toIntern(),
17033 .child = .slice_const_u8_sentinel_0_type,
1678117034 });
1678217035 const new_decl_val = (try pt.aggregateValue(array_decl_ty, decl_vals.items)).toIntern();
1678317036 const slice_ty = (try pt.ptrType(.{
16784 .child = declaration_ty.toIntern(),
17037 .child = .slice_const_u8_sentinel_0_type,
1678517038 .flags = .{
1678617039 .size = .slice,
1678717040 .is_const = true,
......@@ -16805,7 +17058,6 @@ fn typeInfoDecls(
1680517058fn typeInfoNamespaceDecls(
1680617059 sema: *Sema,
1680717060 opt_namespace_index: InternPool.OptionalNamespaceIndex,
16808 declaration_ty: Type,
1680917061 decl_vals: *std.array_list.Managed(InternPool.Index),
1681017062 seen_namespaces: *std.AutoHashMap(*Namespace, void),
1681117063) !void {
......@@ -16850,11 +17102,7 @@ fn typeInfoNamespaceDecls(
1685017102 },
1685117103 });
1685217104 };
16853 const fields = [_]InternPool.Index{
16854 // name: [:0]const u8,
16855 name_val,
16856 };
16857 try decl_vals.append((try pt.aggregateValue(declaration_ty, &fields)).toIntern());
17105 try decl_vals.append(name_val);
1685817106 }
1685917107}
1686017108
......@@ -19470,11 +19718,11 @@ fn zirReifySliceArgTy(
1947019718
1947119719 const comptime_reason: std.zig.SimpleComptimeReason, const in_scalar_ty: Type, const out_scalar_ty: Type = switch (info) {
1947219720 // zig fmt: off
19473 .type_to_fn_param_attrs => .{ .fn_param_attrs, .type, try sema.getStdLangType(src, .@"Type.Fn.Param.Attributes") },
19721 .type_to_fn_param_attrs => .{ .fn_param_attrs, .type, try sema.getStdLangType(src, .@"Type.Fn.ParamAttributes") },
1947419722 .string_to_struct_field_type => .{ .struct_field_types, .slice_const_u8, .type },
1947519723 .string_to_union_field_type => .{ .union_field_types, .slice_const_u8, .type },
19476 .string_to_struct_field_attrs => .{ .struct_field_attrs, .slice_const_u8, try sema.getStdLangType(src, .@"Type.StructField.Attributes") },
19477 .string_to_union_field_attrs => .{ .union_field_attrs, .slice_const_u8, try sema.getStdLangType(src, .@"Type.UnionField.Attributes") },
19724 .string_to_struct_field_attrs => .{ .struct_field_attrs, .slice_const_u8, try sema.getStdLangType(src, .@"Type.Struct.FieldAttributes") },
19725 .string_to_union_field_attrs => .{ .union_field_attrs, .slice_const_u8, try sema.getStdLangType(src, .@"Type.Union.FieldAttributes") },
1947819726 // zig fmt: on
1947919727 };
1948019728
......@@ -19688,7 +19936,7 @@ fn zirReifyFn(
1968819936 const ret_ty_src = block.builtinCallArgSrc(extra.node, 2);
1968919937 const fn_attrs_src = block.builtinCallArgSrc(extra.node, 3);
1969019938
19691 const single_param_attrs_ty = try sema.getStdLangType(param_attrs_src, .@"Type.Fn.Param.Attributes");
19939 const single_param_attrs_ty = try sema.getStdLangType(param_attrs_src, .@"Type.Fn.ParamAttributes");
1969219940 const fn_attrs_ty = try sema.getStdLangType(fn_attrs_src, .@"Type.Fn.Attributes");
1969319941
1969419942 const param_types_uncoerced = sema.resolveInst(extra.param_types);
......@@ -19827,7 +20075,7 @@ fn zirReifyStruct(
1982720075 };
1982820076
1982920077 const container_layout_ty = try sema.getStdLangType(layout_src, .@"Type.ContainerLayout");
19830 const single_field_attrs_ty = try sema.getStdLangType(field_attrs_src, .@"Type.StructField.Attributes");
20078 const single_field_attrs_ty = try sema.getStdLangType(field_attrs_src, .@"Type.Struct.FieldAttributes");
1983120079
1983220080 const layout_uncoerced = sema.resolveInst(extra.layout);
1983320081 const layout_coerced = try sema.coerce(block, container_layout_ty, layout_uncoerced, layout_src);
......@@ -20107,7 +20355,7 @@ fn zirReifyUnion(
2010720355 };
2010820356
2010920357 const container_layout_ty = try sema.getStdLangType(layout_src, .@"Type.ContainerLayout");
20110 const single_field_attrs_ty = try sema.getStdLangType(field_attrs_src, .@"Type.UnionField.Attributes");
20358 const single_field_attrs_ty = try sema.getStdLangType(field_attrs_src, .@"Type.Union.FieldAttributes");
2011120359
2011220360 const layout_uncoerced = sema.resolveInst(extra.layout);
2011320361 const layout_coerced = try sema.coerce(block, container_layout_ty, layout_uncoerced, layout_src);
src/Zcu.zig+9-19
......@@ -448,8 +448,7 @@ pub const StdLangDecl = enum {
448448
449449 Type,
450450 @"Type.Fn",
451 @"Type.Fn.Param",
452 @"Type.Fn.Param.Attributes",
451 @"Type.Fn.ParamAttributes",
453452 @"Type.Fn.Attributes",
454453 @"Type.Int",
455454 @"Type.Float",
......@@ -459,20 +458,16 @@ pub const StdLangDecl = enum {
459458 @"Type.Array",
460459 @"Type.Vector",
461460 @"Type.Optional",
462 @"Type.Error",
463461 @"Type.ErrorUnion",
464 @"Type.EnumField",
462 @"Type.ErrorSet",
465463 @"Type.Enum",
466464 @"Type.Enum.Mode",
467465 @"Type.Union",
468 @"Type.UnionField",
469 @"Type.UnionField.Attributes",
466 @"Type.Union.FieldAttributes",
470467 @"Type.Struct",
471 @"Type.StructField",
472 @"Type.StructField.Attributes",
468 @"Type.Struct.FieldAttributes",
473469 @"Type.ContainerLayout",
474470 @"Type.Opaque",
475 @"Type.Declaration",
476471
477472 panic,
478473 @"panic.call",
......@@ -533,8 +528,7 @@ pub const StdLangDecl = enum {
533528
534529 .Type,
535530 .@"Type.Fn",
536 .@"Type.Fn.Param",
537 .@"Type.Fn.Param.Attributes",
531 .@"Type.Fn.ParamAttributes",
538532 .@"Type.Fn.Attributes",
539533 .@"Type.Int",
540534 .@"Type.Float",
......@@ -544,20 +538,16 @@ pub const StdLangDecl = enum {
544538 .@"Type.Array",
545539 .@"Type.Vector",
546540 .@"Type.Optional",
547 .@"Type.Error",
548541 .@"Type.ErrorUnion",
549 .@"Type.EnumField",
542 .@"Type.ErrorSet",
550543 .@"Type.Enum",
551544 .@"Type.Enum.Mode",
552545 .@"Type.Union",
553 .@"Type.UnionField",
554 .@"Type.UnionField.Attributes",
546 .@"Type.Union.FieldAttributes",
555547 .@"Type.Struct",
556 .@"Type.StructField",
557 .@"Type.StructField.Attributes",
548 .@"Type.Struct.FieldAttributes",
558549 .@"Type.ContainerLayout",
559550 .@"Type.Opaque",
560 .@"Type.Declaration",
561551 => .type,
562552
563553 .panic => .type,
......@@ -611,7 +601,7 @@ pub const StdLangDecl = enum {
611601 .VaList => .va_list,
612602 .assembly, .@"assembly.Clobbers" => .assembly,
613603 else => {
614 if (@intFromEnum(decl) <= @intFromEnum(StdLangDecl.@"Type.Declaration")) {
604 if (@intFromEnum(decl) <= @intFromEnum(StdLangDecl.@"Type.Opaque")) {
615605 return .main;
616606 } else {
617607 return .panic;