authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-01 12:53:57-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-02 18:31:18-04:00
log3dc8448680cfea2b55a6064c655e400e31e5d3dd
treeb8fc52931b21831c5346a956824abcf52011519f
parentee5064c053526e9e6a0a94d835cd334eea2e5823
signaturelock-open Commit is signed but in an unrecognized format.

introduce lazy values

but I think it's a bad idea, so I'm going to back out the change

6 files changed, 681 insertions(+), 264 deletions(-)

src/all_types.hpp+42-2
......@@ -256,6 +256,7 @@ enum ConstValSpecial {
256256 ConstValSpecialRuntime,
257257 ConstValSpecialStatic,
258258 ConstValSpecialUndef,
259 ConstValSpecialLazy,
259260};
260261
261262enum RuntimeHintErrorUnion {
......@@ -291,6 +292,43 @@ struct ConstGlobalRefs {
291292 LLVMValueRef llvm_global;
292293};
293294
295enum LazyValueId {
296 LazyValueIdInvalid,
297 LazyValueIdAlignOf,
298 LazyValueIdSliceType,
299 LazyValueIdFnType,
300};
301
302struct LazyValue {
303 LazyValueId id;
304 IrExecutable *exec;
305};
306
307struct LazyValueAlignOf {
308 LazyValue base;
309 ZigType *target_type;
310};
311
312struct LazyValueSliceType {
313 LazyValue base;
314 ZigType *elem_type;
315 ConstExprValue *align_val; // can be null
316 bool is_const;
317 bool is_volatile;
318 bool is_allowzero;
319};
320
321struct LazyValueFnType {
322 LazyValue base;
323 AstNode *proto_node;
324 ConstExprValue **param_types;
325 ConstExprValue *align_val; // can be null
326 ConstExprValue *return_type;
327 ConstExprValue *async_allocator_type;
328 bool is_generic;
329 bool is_var_args;
330};
331
294332struct ConstExprValue {
295333 ZigType *type;
296334 ConstValSpecial special;
......@@ -318,6 +356,7 @@ struct ConstExprValue {
318356 ConstPtrValue x_ptr;
319357 ConstArgTuple x_arg_tuple;
320358 Buf *x_enum_literal;
359 LazyValue *x_lazy;
321360
322361 // populated if special == ConstValSpecialRuntime
323362 RuntimeHintErrorUnion rh_error_union;
......@@ -359,6 +398,7 @@ enum TldResolution {
359398 TldResolutionUnresolved,
360399 TldResolutionResolving,
361400 TldResolutionInvalid,
401 TldResolutionOkLazy,
362402 TldResolutionOk,
363403};
364404
......@@ -1064,7 +1104,8 @@ struct ZigTypeArray {
10641104
10651105struct TypeStructField {
10661106 Buf *name;
1067 ZigType *type_entry;
1107 ZigType *type_entry; // available after ResolveStatusSizeKnown
1108 ConstExprValue *type_val; // available after ResolveStatusZeroBitsKnown
10681109 size_t src_index;
10691110 size_t gen_index;
10701111 size_t offset; // byte offset from beginning of struct
......@@ -1893,7 +1934,6 @@ struct ZigVar {
18931934 AstNode *decl_node;
18941935 ZigLLVMDILocalVariable *di_loc_var;
18951936 size_t src_arg_index;
1896 size_t gen_arg_index;
18971937 Scope *parent_scope;
18981938 Scope *child_scope;
18991939 LLVMValueRef param_value_ref;
src/analyze.cpp+286-111
......@@ -19,7 +19,6 @@
1919
2020static const size_t default_backward_branch_quota = 1000;
2121
22static Error resolve_enum_type(CodeGen *g, ZigType *enum_type);
2322static Error resolve_struct_type(CodeGen *g, ZigType *struct_type);
2423
2524static Error ATTRIBUTE_MUST_USE resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type);
......@@ -568,12 +567,11 @@ static size_t align_forward(size_t addr, size_t alignment) {
568567 return (addr + alignment - 1) & ~(alignment - 1);
569568}
570569
571static size_t next_field_offset(size_t offset, size_t align_from_zero, size_t field_size, size_t field_align) {
572 BREAKPOINT; // TODO test this
570static size_t next_field_offset(size_t offset, size_t align_from_zero, size_t field_size, size_t next_field_align) {
573571 // Convert offset to a pretend address which has the specified alignment.
574572 size_t addr = offset + align_from_zero;
575573 // March the address forward to respect the field alignment.
576 size_t aligned_addr = align_forward(addr + field_size, field_align);
574 size_t aligned_addr = align_forward(addr + field_size, next_field_align);
577575 // Convert back from pretend address to offset.
578576 return aligned_addr - align_from_zero;
579577}
......@@ -623,8 +621,8 @@ ZigType *get_error_union_type(CodeGen *g, ZigType *err_set_type, ZigType *payloa
623621 field_aligns[err_union_err_index] = err_set_type->abi_align;
624622 field_sizes[err_union_payload_index] = payload_type->abi_size;
625623 field_aligns[err_union_payload_index] = payload_type->abi_align;
626 size_t field2_offset = next_field_offset(0, entry->abi_align, field_sizes[0], field_aligns[0]);
627 entry->abi_size = next_field_offset(field2_offset, entry->abi_align, field_sizes[1], field_aligns[1]);
624 size_t field2_offset = next_field_offset(0, entry->abi_align, field_sizes[0], field_aligns[1]);
625 entry->abi_size = next_field_offset(field2_offset, entry->abi_align, field_sizes[1], entry->abi_align);
628626 entry->size_in_bits = entry->abi_size * 8;
629627 }
630628
......@@ -699,6 +697,15 @@ ZigType *get_slice_type(CodeGen *g, ZigType *ptr_type) {
699697 entry->data.structure.fields_by_name.put(ptr_field_name, &entry->data.structure.fields[slice_ptr_index]);
700698 entry->data.structure.fields_by_name.put(len_field_name, &entry->data.structure.fields[slice_len_index]);
701699
700 switch (type_requires_comptime(g, ptr_type)) {
701 case ReqCompTimeInvalid:
702 zig_unreachable();
703 case ReqCompTimeNo:
704 break;
705 case ReqCompTimeYes:
706 entry->data.structure.requires_comptime = true;
707 }
708
702709 if (!type_has_bits(ptr_type)) {
703710 entry->data.structure.gen_field_count = 1;
704711 entry->data.structure.fields[slice_ptr_index].gen_index = SIZE_MAX;
......@@ -897,8 +904,8 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
897904
898905 fn_type->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
899906 fn_type->abi_size = g->builtin_types.entry_usize->abi_size;
900 fn_type->abi_align = (fn_type_id->alignment == 0) ?
901 g->builtin_types.entry_usize->abi_align : fn_type_id->alignment;
907 // see also type_val_resolve_abi_align
908 fn_type->abi_align = (fn_type_id->alignment == 0) ? 1 : fn_type_id->alignment;
902909
903910 g->fn_type_table.put(&fn_type->data.fn.fn_type_id, fn_type);
904911
......@@ -956,15 +963,134 @@ ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind
956963 return entry;
957964}
958965
959static ConstExprValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry,
960 Buf *type_name)
966static ConstExprValue *analyze_const_value_allow_lazy(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry,
967 Buf *type_name, bool allow_lazy)
961968{
962969 size_t backward_branch_count = 0;
963970 return ir_eval_const_value(g, scope, node, type_entry,
964971 &backward_branch_count, default_backward_branch_quota,
965 nullptr, nullptr, node, type_name, nullptr, nullptr);
972 nullptr, nullptr, node, type_name, nullptr, nullptr, allow_lazy);
966973}
967974
975static ConstExprValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry,
976 Buf *type_name)
977{
978 return analyze_const_value_allow_lazy(g, scope, node, type_entry, type_name, false);
979}
980
981static Error type_val_resolve_zero_bits(CodeGen *g, ConstExprValue *type_val, bool *is_zero_bits) {
982 Error err;
983 if (type_val->special != ConstValSpecialLazy) {
984 assert(type_val->special == ConstValSpecialStatic);
985 if ((err = type_resolve(g, type_val->data.x_type, ResolveStatusZeroBitsKnown)))
986 return err;
987 *is_zero_bits = (type_val->data.x_type->abi_size == 0);
988 return ErrorNone;
989 }
990 switch (type_val->data.x_lazy->id) {
991 case LazyValueIdInvalid:
992 case LazyValueIdAlignOf:
993 zig_unreachable();
994 case LazyValueIdSliceType:
995 *is_zero_bits = false;
996 return ErrorNone;
997 case LazyValueIdFnType: {
998 LazyValueFnType *lazy_fn_type = reinterpret_cast<LazyValueFnType *>(type_val->data.x_lazy);
999 *is_zero_bits = lazy_fn_type->is_generic;
1000 return ErrorNone;
1001 }
1002 }
1003 zig_unreachable();
1004}
1005
1006static Error type_val_resolve_is_opaque_type(CodeGen *g, ConstExprValue *type_val, bool *is_opaque_type) {
1007 if (type_val->special != ConstValSpecialLazy) {
1008 assert(type_val->special == ConstValSpecialStatic);
1009 *is_opaque_type = (type_val->data.x_type->id == ZigTypeIdOpaque);
1010 return ErrorNone;
1011 }
1012 switch (type_val->data.x_lazy->id) {
1013 case LazyValueIdInvalid:
1014 case LazyValueIdAlignOf:
1015 zig_unreachable();
1016 case LazyValueIdSliceType:
1017 case LazyValueIdFnType:
1018 *is_opaque_type = false;
1019 return ErrorNone;
1020 }
1021 zig_unreachable();
1022}
1023
1024static ReqCompTime type_val_resolve_requires_comptime(CodeGen *g, ConstExprValue *type_val) {
1025 if (type_val->special != ConstValSpecialLazy) {
1026 return type_requires_comptime(g, type_val->data.x_type);
1027 }
1028 switch (type_val->data.x_lazy->id) {
1029 case LazyValueIdInvalid:
1030 case LazyValueIdAlignOf:
1031 zig_unreachable();
1032 case LazyValueIdSliceType: {
1033 LazyValueSliceType *lazy_slice_type = reinterpret_cast<LazyValueSliceType *>(type_val->data.x_lazy);
1034 return type_requires_comptime(g, lazy_slice_type->elem_type);
1035 }
1036 case LazyValueIdFnType: {
1037 LazyValueFnType *lazy_fn_type = reinterpret_cast<LazyValueFnType *>(type_val->data.x_lazy);
1038 if (lazy_fn_type->is_generic)
1039 return ReqCompTimeYes;
1040 switch (type_val_resolve_requires_comptime(g, lazy_fn_type->return_type)) {
1041 case ReqCompTimeInvalid:
1042 return ReqCompTimeInvalid;
1043 case ReqCompTimeYes:
1044 return ReqCompTimeYes;
1045 case ReqCompTimeNo:
1046 break;
1047 }
1048 size_t param_count = lazy_fn_type->proto_node->data.fn_proto.params.length;
1049 if (lazy_fn_type->is_var_args) param_count -= 1;
1050 for (size_t i = 0; i < param_count; i += 1) {
1051 switch (type_val_resolve_requires_comptime(g, lazy_fn_type->param_types[i])) {
1052 case ReqCompTimeInvalid:
1053 return ReqCompTimeInvalid;
1054 case ReqCompTimeYes:
1055 return ReqCompTimeYes;
1056 case ReqCompTimeNo:
1057 break;
1058 }
1059 }
1060 return ReqCompTimeNo;
1061 }
1062 }
1063 zig_unreachable();
1064}
1065
1066static Error type_val_resolve_abi_align(CodeGen *g, ConstExprValue *type_val, size_t *abi_align) {
1067 Error err;
1068 if (type_val->special != ConstValSpecialLazy) {
1069 assert(type_val->special == ConstValSpecialStatic);
1070 if ((err = type_resolve(g, type_val->data.x_type, ResolveStatusAlignmentKnown)))
1071 return err;
1072 *abi_align = type_val->data.x_type->abi_align;
1073 return ErrorNone;
1074 }
1075 switch (type_val->data.x_lazy->id) {
1076 case LazyValueIdInvalid:
1077 case LazyValueIdAlignOf:
1078 zig_unreachable();
1079 case LazyValueIdSliceType:
1080 *abi_align = g->builtin_types.entry_usize->abi_align;
1081 return ErrorNone;
1082 case LazyValueIdFnType: {
1083 LazyValueFnType *lazy_fn_type = reinterpret_cast<LazyValueFnType *>(type_val->data.x_lazy);
1084 if (lazy_fn_type->align_val != nullptr)
1085 return type_val_resolve_abi_align(g, lazy_fn_type->align_val, abi_align);
1086 *abi_align = 1;
1087 return ErrorNone;
1088 }
1089 }
1090 zig_unreachable();
1091}
1092
1093
9681094ZigType *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) {
9691095 ConstExprValue *result = analyze_const_value(g, scope, node, g->builtin_types.entry_type, nullptr);
9701096 if (type_is_invalid(result->type))
......@@ -1492,11 +1618,6 @@ bool type_is_invalid(ZigType *type_entry) {
14921618}
14931619
14941620
1495static Error resolve_enum_type(CodeGen *g, ZigType *enum_type) {
1496 return resolve_enum_zero_bits(g, enum_type);
1497}
1498
1499
15001621ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_names[],
15011622 ZigType *field_types[], size_t field_count)
15021623{
......@@ -1536,8 +1657,9 @@ ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_na
15361657 for (size_t i = 0; i < field_count; i += 1) {
15371658 TypeStructField *field = &struct_type->data.structure.fields[i];
15381659 field->offset = next_offset;
1539 next_offset = next_field_offset(next_offset, abi_align,
1540 field->type_entry->abi_size, field->type_entry->abi_align);
1660 size_t next_abi_align = (i + 1 == field_count) ?
1661 abi_align : struct_type->data.structure.fields[i + 1].type_entry->abi_align;
1662 next_offset = next_field_offset(next_offset, abi_align, field->type_entry->abi_size, next_abi_align);
15411663 }
15421664
15431665 struct_type->abi_align = abi_align;
......@@ -1548,7 +1670,7 @@ ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_na
15481670}
15491671
15501672static size_t get_store_size_in_bits(size_t size_in_bits) {
1551 return (size_in_bits + 7) / 8;
1673 return ((size_in_bits + 7) / 8) * 8;
15521674}
15531675
15541676static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
......@@ -1584,7 +1706,7 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
15841706 bool packed = (struct_type->data.structure.layout == ContainerLayoutPacked);
15851707 struct_type->data.structure.resolve_loop_flag = true;
15861708
1587 uint32_t *host_int_bytes = allocate<uint32_t>(struct_type->data.structure.gen_field_count);
1709 uint32_t *host_int_bytes = packed ? allocate<uint32_t>(struct_type->data.structure.gen_field_count) : nullptr;
15881710
15891711 // Compute offsets for all the fields.
15901712 size_t packed_bits_offset = 0;
......@@ -1594,24 +1716,53 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
15941716 size_t size_in_bits = 0;
15951717 size_t abi_align = struct_type->abi_align;
15961718
1719 // Resolve types for fields
15971720 for (size_t i = 0; i < field_count; i += 1) {
1598 TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
1599 ZigType *field_type = type_struct_field->type_entry;
1721 AstNode *field_source_node = decl_node->data.container_decl.fields.at(i);
1722 TypeStructField *field = &struct_type->data.structure.fields[i];
16001723
1601 if (!type_has_bits(field_type))
1602 continue;
1724 if ((err = ir_resolve_lazy(g, field_source_node, field->type_val))) {
1725 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
1726 return err;
1727 }
1728 ZigType *field_type = field->type_val->data.x_type;
1729 field->type_entry = field_type;
16031730
16041731 if ((err = type_resolve(g, field_type, ResolveStatusSizeKnown))) {
16051732 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
1606 return ErrorSemanticAnalyzeFail;
1733 return err;
16071734 }
16081735
16091736 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid) {
16101737 return ErrorSemanticAnalyzeFail;
16111738 }
16121739
1613 type_struct_field->gen_index = gen_field_index;
1614 type_struct_field->offset = next_offset;
1740 if (packed) {
1741 if ((err = emit_error_unless_type_allowed_in_packed_struct(g, field_type, field_source_node))) {
1742 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
1743 return ErrorSemanticAnalyzeFail;
1744 }
1745 } else if (struct_type->data.structure.layout == ContainerLayoutExtern &&
1746 !type_allowed_in_extern(g, field_type))
1747 {
1748 add_node_error(g, field_source_node,
1749 buf_sprintf("extern structs cannot contain fields of type '%s'",
1750 buf_ptr(&field_type->name)));
1751 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
1752 return ErrorSemanticAnalyzeFail;
1753 }
1754 }
1755
1756 // Calculate offsets
1757 for (size_t i = 0; i < field_count; i += 1) {
1758 TypeStructField *field = &struct_type->data.structure.fields[i];
1759 if (field->gen_index == SIZE_MAX)
1760 continue;
1761 ZigType *field_type = field->type_entry;
1762 assert(field_type != nullptr);
1763
1764 field->gen_index = gen_field_index;
1765 field->offset = next_offset;
16151766
16161767 if (packed) {
16171768 size_t field_size_in_bits = type_size_bits(g, field_type);
......@@ -1621,7 +1772,7 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
16211772
16221773 if (first_packed_bits_offset_misalign != SIZE_MAX) {
16231774 // this field is not byte-aligned; it is part of the previous field with a bit offset
1624 type_struct_field->bit_offset_in_host = packed_bits_offset - first_packed_bits_offset_misalign;
1775 field->bit_offset_in_host = packed_bits_offset - first_packed_bits_offset_misalign;
16251776
16261777 size_t full_bit_count = next_packed_bits_offset - first_packed_bits_offset_misalign;
16271778 if (get_store_size_in_bits(full_bit_count) == full_bit_count) {
......@@ -1636,10 +1787,10 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
16361787 }
16371788 } else if (get_store_size_in_bits(field_type->size_in_bits) != field_size_in_bits) {
16381789 first_packed_bits_offset_misalign = packed_bits_offset;
1639 type_struct_field->bit_offset_in_host = 0;
1790 field->bit_offset_in_host = 0;
16401791 } else {
16411792 // This is a byte-aligned field (both start and end) in a packed struct.
1642 type_struct_field->bit_offset_in_host = 0;
1793 field->bit_offset_in_host = 0;
16431794 gen_field_index += 1;
16441795 // TODO: https://github.com/ziglang/zig/issues/1512
16451796 next_offset = next_field_offset(next_offset, abi_align, field_type->size_in_bits / 8, 1);
......@@ -1648,7 +1799,15 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
16481799 packed_bits_offset = next_packed_bits_offset;
16491800 } else {
16501801 gen_field_index += 1;
1651 next_offset = next_field_offset(next_offset, abi_align, field_type->abi_size, field_type->abi_align);
1802 size_t next_src_field_index = i + 1;
1803 for (; next_src_field_index < field_count; next_src_field_index += 1) {
1804 if (struct_type->data.structure.fields[next_src_field_index].gen_index != SIZE_MAX) {
1805 break;
1806 }
1807 }
1808 size_t next_abi_align = (next_src_field_index == field_count) ?
1809 abi_align : struct_type->data.structure.fields[next_src_field_index].type_entry->abi_align;
1810 next_offset = next_field_offset(next_offset, abi_align, field_type->abi_size, next_abi_align);
16521811 size_in_bits = next_offset * 8;
16531812 }
16541813 }
......@@ -1679,9 +1838,10 @@ static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) {
16791838 return ErrorSemanticAnalyzeFail;
16801839 if (union_type->data.unionation.resolve_status >= ResolveStatusAlignmentKnown)
16811840 return ErrorNone;
1682
16831841 if ((err = resolve_union_zero_bits(g, union_type)))
16841842 return err;
1843 if (union_type->data.unionation.resolve_status >= ResolveStatusAlignmentKnown)
1844 return ErrorNone;
16851845
16861846 if (union_type->data.unionation.resolve_loop_flag) {
16871847 if (!union_type->data.unionation.reported_infinite_err) {
......@@ -1724,7 +1884,7 @@ static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) {
17241884 }
17251885
17261886 // unset temporary flag
1727 union_type->data.unionation.resolve_loop_flag = true;
1887 union_type->data.unionation.resolve_loop_flag = false;
17281888 union_type->data.unionation.resolve_status = ResolveStatusAlignmentKnown;
17291889
17301890 ZigType *tag_type = union_type->data.unionation.tag_type;
......@@ -1836,8 +1996,8 @@ static Error resolve_union_type(CodeGen *g, ZigType *union_type) {
18361996 field_aligns[union_type->data.unionation.gen_tag_index] = tag_type->abi_align;
18371997 field_sizes[union_type->data.unionation.gen_union_index] = union_abi_size;
18381998 field_aligns[union_type->data.unionation.gen_union_index] = most_aligned_union_member->abi_align;
1839 size_t field2_offset = next_field_offset(0, union_type->abi_align, field_sizes[0], field_aligns[0]);
1840 union_type->abi_size = next_field_offset(field2_offset, union_type->abi_align, field_sizes[1], field_aligns[1]);
1999 size_t field2_offset = next_field_offset(0, union_type->abi_align, field_sizes[0], field_aligns[1]);
2000 union_type->abi_size = next_field_offset(field2_offset, union_type->abi_align, field_sizes[1], union_type->abi_align);
18412001 union_type->size_in_bits = union_type->abi_size * 8;
18422002 }
18432003 } else {
......@@ -1928,6 +2088,9 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
19282088 }
19292089 }
19302090 enum_type->data.enumeration.tag_int_type = tag_int_type;
2091 enum_type->size_in_bits = tag_int_type->size_in_bits;
2092 enum_type->abi_size = tag_int_type->abi_size;
2093 enum_type->abi_align = tag_int_type->abi_align;
19312094
19322095 for (uint32_t field_i = 0; field_i < field_count; field_i += 1) {
19332096 AstNode *field_node = decl_node->data.container_decl.fields.at(field_i);
......@@ -2012,6 +2175,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
20122175
20132176 enum_type->data.enumeration.zero_bits_loop_flag = false;
20142177 enum_type->data.enumeration.zero_bits_known = true;
2178 enum_type->data.enumeration.complete = true;
20152179
20162180 if (enum_type->data.enumeration.is_invalid)
20172181 return ErrorSemanticAnalyzeFail;
......@@ -2022,22 +2186,28 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
20222186static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
20232187 assert(struct_type->id == ZigTypeIdStruct);
20242188
2189 Error err;
2190
20252191 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
20262192 return ErrorSemanticAnalyzeFail;
20272193 if (struct_type->data.structure.resolve_status >= ResolveStatusZeroBitsKnown)
20282194 return ErrorNone;
20292195
2196 AstNode *decl_node = struct_type->data.structure.decl_node;
2197 assert(decl_node->type == NodeTypeContainerDecl);
2198
20302199 if (struct_type->data.structure.resolve_loop_flag) {
2031 struct_type->data.structure.resolve_status = ResolveStatusZeroBitsKnown;
2032 struct_type->data.structure.resolve_loop_flag = false;
2033 return ErrorNone;
2200 if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) {
2201 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2202 ErrorMsg *msg = add_node_error(g, decl_node,
2203 buf_sprintf("struct '%s' depends on its own size", buf_ptr(&struct_type->name)));
2204 emit_error_notes_for_ref_stack(g, msg);
2205 }
2206 return ErrorSemanticAnalyzeFail;
20342207 }
20352208
20362209 struct_type->data.structure.resolve_loop_flag = true;
20372210
2038 AstNode *decl_node = struct_type->data.structure.decl_node;
2039 assert(decl_node->type == NodeTypeContainerDecl);
2040
20412211 assert(!struct_type->data.structure.fields);
20422212 size_t field_count = decl_node->data.container_decl.fields.length;
20432213 struct_type->data.structure.src_field_count = (uint32_t)field_count;
......@@ -2056,7 +2226,7 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
20562226 if (field_node->data.struct_field.type == nullptr) {
20572227 add_node_error(g, field_node, buf_sprintf("struct field missing type"));
20582228 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2059 continue;
2229 return ErrorSemanticAnalyzeFail;
20602230 }
20612231
20622232 auto field_entry = struct_type->data.structure.fields_by_name.put_unique(type_struct_field->name, type_struct_field);
......@@ -2065,38 +2235,55 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
20652235 buf_sprintf("duplicate struct field: '%s'", buf_ptr(type_struct_field->name)));
20662236 add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here"));
20672237 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2068 continue;
2238 return ErrorSemanticAnalyzeFail;
20692239 }
20702240
2071 ZigType *field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type);
2072 type_struct_field->type_entry = field_type;
2241 ConstExprValue *field_type_val = analyze_const_value_allow_lazy(g, scope,
2242 field_node->data.struct_field.type, g->builtin_types.entry_type, nullptr, true);
2243 if (type_is_invalid(field_type_val->type)) {
2244 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2245 return ErrorSemanticAnalyzeFail;
2246 }
2247 assert(field_type_val->special != ConstValSpecialRuntime);
2248 type_struct_field->type_val = field_type_val;
20732249 type_struct_field->src_index = i;
20742250 type_struct_field->gen_index = SIZE_MAX;
20752251
2252 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
2253 return ErrorSemanticAnalyzeFail;
2254
20762255 if (field_node->data.struct_field.value != nullptr) {
20772256 add_node_error(g, field_node->data.struct_field.value,
20782257 buf_sprintf("enums, not structs, support field assignment"));
20792258 }
2080
2081 if (field_type->id == ZigTypeIdOpaque) {
2259 bool field_is_opaque_type;
2260 if ((err = type_val_resolve_is_opaque_type(g, field_type_val, &field_is_opaque_type))) {
2261 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2262 return ErrorSemanticAnalyzeFail;
2263 }
2264 if (field_is_opaque_type) {
20822265 add_node_error(g, field_node->data.struct_field.type,
20832266 buf_sprintf("opaque types have unknown size and therefore cannot be directly embedded in structs"));
20842267 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2085 continue;
2268 return ErrorSemanticAnalyzeFail;
20862269 }
2087
2088 switch (type_requires_comptime(g, field_type)) {
2270 switch (type_val_resolve_requires_comptime(g, field_type_val)) {
20892271 case ReqCompTimeYes:
20902272 struct_type->data.structure.requires_comptime = true;
20912273 break;
20922274 case ReqCompTimeInvalid:
20932275 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2094 continue;
2276 return ErrorSemanticAnalyzeFail;
20952277 case ReqCompTimeNo:
20962278 break;
20972279 }
20982280
2099 if (!type_has_bits(field_type))
2281 bool field_is_zero_bits;
2282 if ((err = type_val_resolve_zero_bits(g, field_type_val, &field_is_zero_bits))) {
2283 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2284 return ErrorSemanticAnalyzeFail;
2285 }
2286 if (field_is_zero_bits)
21002287 continue;
21012288
21022289 type_struct_field->gen_index = gen_field_index;
......@@ -2126,9 +2313,10 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
21262313 return ErrorSemanticAnalyzeFail;
21272314 if (struct_type->data.structure.resolve_status >= ResolveStatusAlignmentKnown)
21282315 return ErrorNone;
2129
21302316 if ((err = resolve_struct_zero_bits(g, struct_type)))
21312317 return err;
2318 if (struct_type->data.structure.resolve_status >= ResolveStatusAlignmentKnown)
2319 return ErrorNone;
21322320
21332321 AstNode *decl_node = struct_type->data.structure.decl_node;
21342322
......@@ -2136,7 +2324,7 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
21362324 if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) {
21372325 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
21382326 ErrorMsg *msg = add_node_error(g, decl_node,
2139 buf_sprintf("struct '%s' contains itself", buf_ptr(&struct_type->name)));
2327 buf_sprintf("struct '%s' depends on its own alignment", buf_ptr(&struct_type->name)));
21402328 emit_error_notes_for_ref_stack(g, msg);
21412329 }
21422330 return ErrorSemanticAnalyzeFail;
......@@ -2151,42 +2339,23 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
21512339
21522340 for (size_t i = 0; i < field_count; i += 1) {
21532341 TypeStructField *field = &struct_type->data.structure.fields[i];
2154 ZigType *field_type = field->type_entry;
2155 assert(field_type != nullptr);
2156
2157 if ((err = type_resolve(g, field_type, ResolveStatusAlignmentKnown))) {
2158 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2159 return ErrorSemanticAnalyzeFail;
2160 }
2161
2162 if (struct_type->data.structure.layout == ContainerLayoutExtern &&
2163 !type_allowed_in_extern(g, field_type))
2164 {
2165 AstNode *field_source_node = decl_node->data.container_decl.fields.at(i);
2166 add_node_error(g, field_source_node,
2167 buf_sprintf("extern structs cannot contain fields of type '%s'",
2168 buf_ptr(&field_type->name)));
2169 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2170 return ErrorSemanticAnalyzeFail;
2171 }
2172
2173 if (!type_has_bits(field_type))
2342 if (field->gen_index == SIZE_MAX)
21742343 continue;
21752344
21762345 if (packed) {
2177 AstNode *field_source_node = decl_node->data.container_decl.fields.at(i);
2178 if ((err = emit_error_unless_type_allowed_in_packed_struct(g, field_type, field_source_node))) {
2179 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2180 return ErrorSemanticAnalyzeFail;
2181 }
21822346 // TODO: https://github.com/ziglang/zig/issues/1512
21832347 if (1 > abi_align) {
21842348 abi_align = 1;
21852349 }
21862350 } else {
21872351 // TODO: https://github.com/ziglang/zig/issues/1512
2188 if (field_type->abi_align > abi_align) {
2189 abi_align = field_type->abi_align;
2352 size_t field_align;
2353 if ((err = type_val_resolve_abi_align(g, field->type_val, &field_align))) {
2354 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2355 return err;
2356 }
2357 if (field_align > abi_align) {
2358 abi_align = field_align;
21902359 }
21912360 }
21922361 }
......@@ -2824,7 +2993,7 @@ void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source
28242993
28252994void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value) {
28262995 Tld *tld = get_container_scope(g->compile_var_import)->decl_table.get(name);
2827 resolve_top_level_decl(g, tld, tld->source_node);
2996 resolve_top_level_decl(g, tld, tld->source_node, false);
28282997 assert(tld->id == TldIdVar);
28292998 TldVar *tld_var = (TldVar *)tld;
28302999 tld_var->var->const_value = value;
......@@ -2933,20 +3102,17 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
29333102 }
29343103}
29353104
2936static void resolve_decl_container(CodeGen *g, TldContainer *tld_container) {
3105static Error resolve_decl_container(CodeGen *g, TldContainer *tld_container) {
29373106 ZigType *type_entry = tld_container->type_entry;
29383107 assert(type_entry);
29393108
29403109 switch (type_entry->id) {
29413110 case ZigTypeIdStruct:
2942 resolve_struct_type(g, tld_container->type_entry);
2943 return;
3111 return resolve_struct_type(g, tld_container->type_entry);
29443112 case ZigTypeIdEnum:
2945 resolve_enum_type(g, tld_container->type_entry);
2946 return;
3113 return resolve_enum_zero_bits(g, tld_container->type_entry);
29473114 case ZigTypeIdUnion:
2948 resolve_union_type(g, tld_container->type_entry);
2949 return;
3115 return resolve_union_type(g, tld_container->type_entry);
29503116 default:
29513117 zig_unreachable();
29523118 }
......@@ -3066,7 +3232,7 @@ ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf
30663232 return variable_entry;
30673233}
30683234
3069static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
3235static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) {
30703236 AstNode *source_node = tld_var->base.source_node;
30713237 AstNodeVariableDeclaration *var_decl = &source_node->data.variable_declaration;
30723238
......@@ -3107,7 +3273,8 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
31073273 if (explicit_type && explicit_type->id == ZigTypeIdInvalid) {
31083274 implicit_type = explicit_type;
31093275 } else if (var_decl->expr) {
3110 init_value = analyze_const_value(g, tld_var->base.parent_scope, var_decl->expr, explicit_type, var_decl->symbol);
3276 init_value = analyze_const_value_allow_lazy(g, tld_var->base.parent_scope, var_decl->expr,
3277 explicit_type, var_decl->symbol, allow_lazy);
31113278 assert(init_value);
31123279 implicit_type = init_value->type;
31133280
......@@ -3170,11 +3337,11 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
31703337 g->global_vars.append(tld_var);
31713338}
31723339
3173void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node) {
3174 if (tld->resolution != TldResolutionUnresolved)
3340void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node, bool allow_lazy) {
3341 bool want_resolve_lazy = tld->resolution == TldResolutionOkLazy && !allow_lazy;
3342 if (tld->resolution != TldResolutionUnresolved && !want_resolve_lazy)
31753343 return;
31763344
3177 assert(tld->resolution != TldResolutionResolving);
31783345 tld->resolution = TldResolutionResolving;
31793346 g->tld_ref_source_node_stack.append(source_node);
31803347
......@@ -3182,7 +3349,11 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node) {
31823349 case TldIdVar:
31833350 {
31843351 TldVar *tld_var = (TldVar *)tld;
3185 resolve_decl_var(g, tld_var);
3352 if (want_resolve_lazy) {
3353 ir_resolve_lazy(g, source_node, tld_var->var->const_value);
3354 } else {
3355 resolve_decl_var(g, tld_var, allow_lazy);
3356 }
31863357 break;
31873358 }
31883359 case TldIdFn:
......@@ -3205,7 +3376,7 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node) {
32053376 }
32063377 }
32073378
3208 tld->resolution = TldResolutionOk;
3379 tld->resolution = allow_lazy ? TldResolutionOkLazy : TldResolutionOk;
32093380 g->tld_ref_source_node_stack.pop();
32103381}
32113382
......@@ -3399,17 +3570,14 @@ ZigType *container_ref_type(ZigType *type_entry) {
33993570 type_entry->data.pointer.child_type : type_entry;
34003571}
34013572
3402void resolve_container_type(CodeGen *g, ZigType *type_entry) {
3573Error resolve_container_type(CodeGen *g, ZigType *type_entry) {
34033574 switch (type_entry->id) {
34043575 case ZigTypeIdStruct:
3405 resolve_struct_type(g, type_entry);
3406 break;
3576 return resolve_struct_type(g, type_entry);
34073577 case ZigTypeIdEnum:
3408 resolve_enum_type(g, type_entry);
3409 break;
3578 return resolve_enum_zero_bits(g, type_entry);
34103579 case ZigTypeIdUnion:
3411 resolve_union_type(g, type_entry);
3412 break;
3580 return resolve_union_type(g, type_entry);
34133581 case ZigTypeIdPointer:
34143582 case ZigTypeIdMetaType:
34153583 case ZigTypeIdVoid:
......@@ -3435,6 +3603,7 @@ void resolve_container_type(CodeGen *g, ZigType *type_entry) {
34353603 case ZigTypeIdVector:
34363604 zig_unreachable();
34373605 }
3606 zig_unreachable();
34383607}
34393608
34403609ZigType *get_src_ptr_type(ZigType *type) {
......@@ -3536,10 +3705,6 @@ static void define_local_param_variables(CodeGen *g, ZigFn *fn_table_entry) {
35363705 if (type_has_bits(param_type)) {
35373706 fn_table_entry->variable_list.append(var);
35383707 }
3539
3540 if (fn_type->data.fn.gen_param_info) {
3541 var->gen_arg_index = fn_type->data.fn.gen_param_info[i].gen_index;
3542 }
35433708 }
35443709}
35453710
......@@ -3880,7 +4045,7 @@ void semantic_analyze(CodeGen *g) {
38804045 for (; g->resolve_queue_index < g->resolve_queue.length; g->resolve_queue_index += 1) {
38814046 Tld *tld = g->resolve_queue.at(g->resolve_queue_index);
38824047 AstNode *source_node = nullptr;
3883 resolve_top_level_decl(g, tld, source_node);
4048 resolve_top_level_decl(g, tld, source_node, false);
38844049 }
38854050
38864051 for (; g->fn_defs_index < g->fn_defs.length; g->fn_defs_index += 1) {
......@@ -4941,7 +5106,7 @@ Error type_resolve(CodeGen *g, ZigType *ty, ResolveStatus status) {
49415106 if (ty->id == ZigTypeIdStruct) {
49425107 return resolve_struct_type(g, ty);
49435108 } else if (ty->id == ZigTypeIdEnum) {
4944 return resolve_enum_type(g, ty);
5109 return resolve_enum_zero_bits(g, ty);
49455110 } else if (ty->id == ZigTypeIdUnion) {
49465111 return resolve_union_type(g, ty);
49475112 }
......@@ -5314,6 +5479,9 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
53145479 case ConstValSpecialRuntime:
53155480 buf_appendf(buf, "(runtime value)");
53165481 return;
5482 case ConstValSpecialLazy:
5483 buf_appendf(buf, "(lazy value)");
5484 return;
53175485 case ConstValSpecialUndef:
53185486 buf_appendf(buf, "undefined");
53195487 return;
......@@ -5930,7 +6098,7 @@ bool type_ptr_eql(const ZigType *a, const ZigType *b) {
59306098
59316099ConstExprValue *get_builtin_value(CodeGen *codegen, const char *name) {
59326100 Tld *tld = get_container_scope(codegen->compile_var_import)->decl_table.get(buf_create_from_str(name));
5933 resolve_top_level_decl(codegen, tld, nullptr);
6101 resolve_top_level_decl(codegen, tld, nullptr, false);
59346102 assert(tld->id == TldIdVar);
59356103 TldVar *tld_var = (TldVar *)tld;
59366104 ConstExprValue *var_value = tld_var->var->const_value;
......@@ -6114,6 +6282,8 @@ bool type_is_c_abi_int(CodeGen *g, ZigType *ty) {
61146282uint32_t get_host_int_bytes(CodeGen *g, ZigType *struct_type, TypeStructField *field) {
61156283 assert(struct_type->id == ZigTypeIdStruct);
61166284 assert(type_is_resolved(struct_type, ResolveStatusSizeKnown));
6285 if (struct_type->data.structure.host_int_bytes == nullptr)
6286 return 0;
61176287 return struct_type->data.structure.host_int_bytes[field->gen_index];
61186288}
61196289
......@@ -6374,8 +6544,13 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type) {
63746544 LLVMStructSetBody(struct_type->llvm_type, element_types, (unsigned)gen_field_count, packed);
63756545
63766546 ZigLLVMDIType **di_element_types = allocate<ZigLLVMDIType*>(debug_field_count);
6377
63786547 ZigType *import = get_scope_import(scope);
6548 unsigned dwarf_kind = ZigLLVMTag_DW_structure_type();
6549 struct_type->llvm_di_type = ZigLLVMCreateReplaceableCompositeType(g->dbuilder,
6550 dwarf_kind, buf_ptr(&struct_type->name),
6551 ZigLLVMFileToScope(import->data.structure.root_struct->di_file),
6552 import->data.structure.root_struct->di_file, (unsigned)(decl_node->line + 1));
6553
63796554 size_t debug_field_index = 0;
63806555 for (size_t i = 0; i < field_count; i += 1) {
63816556 AstNode *field_node = decl_node->data.container_decl.fields.at(i);
src/analyze.hpp+3-2
......@@ -61,7 +61,7 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *abs_full_path, Bu
6161ZigVar *find_variable(CodeGen *g, Scope *orig_context, Buf *name, ScopeFnDef **crossed_fndef_scope);
6262Tld *find_decl(CodeGen *g, Scope *scope, Buf *name);
6363Tld *find_container_decl(CodeGen *g, ScopeDecls *decls_scope, Buf *name);
64void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node);
64void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node, bool allow_lazy);
6565
6666ZigType *get_src_ptr_type(ZigType *type);
6767ZigType *get_codegen_ptr_type(ZigType *type);
......@@ -73,7 +73,7 @@ bool type_is_complete(ZigType *type_entry);
7373bool type_is_resolved(ZigType *type_entry, ResolveStatus status);
7474bool type_is_invalid(ZigType *type_entry);
7575bool type_is_global_error_set(ZigType *err_set_type);
76void resolve_container_type(CodeGen *g, ZigType *type_entry);
76Error resolve_container_type(CodeGen *g, ZigType *type_entry);
7777ScopeDecls *get_container_scope(ZigType *type_entry);
7878TypeStructField *find_struct_type_field(ZigType *type_entry, Buf *name);
7979TypeEnumField *find_enum_type_field(ZigType *enum_type, Buf *name);
......@@ -246,4 +246,5 @@ Error create_c_object_cache(CodeGen *g, CacheHash **out_cache_hash, bool verbose
246246
247247LLVMTypeRef get_llvm_type(CodeGen *g, ZigType *type);
248248ZigLLVMDIType *get_llvm_di_type(CodeGen *g, ZigType *type);
249
249250#endif
src/codegen.cpp+13-5
......@@ -483,6 +483,8 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, ZigFn *fn_table_entry) {
483483
484484
485485 ZigType *fn_type = fn_table_entry->type_entry;
486 // Make the raw_type_ref populated
487 (void)get_llvm_type(g, fn_type);
486488 LLVMTypeRef fn_llvm_type = fn_type->data.fn.raw_type_ref;
487489 if (fn_table_entry->body_node == nullptr) {
488490 LLVMValueRef existing_llvm_fn = LLVMGetNamedFunction(g->module, buf_ptr(symbol_name));
......@@ -2285,7 +2287,9 @@ void walk_function_params(CodeGen *g, ZigType *fn_type, FnWalk *fn_walk) {
22852287
22862288 if (!handle_is_ptr(variable->var_type)) {
22872289 clear_debug_source_node(g);
2288 gen_store_untyped(g, LLVMGetParam(llvm_fn, (unsigned)variable->gen_arg_index),
2290 ZigType *fn_type = fn_table_entry->type_entry;
2291 unsigned gen_arg_index = fn_type->data.fn.gen_param_info[variable->src_arg_index].gen_index;
2292 gen_store_untyped(g, LLVMGetParam(llvm_fn, gen_arg_index),
22892293 variable->value_ref, variable->align_bytes, false);
22902294 }
22912295
......@@ -3354,6 +3358,8 @@ static bool value_is_all_undef_array(ConstExprValue *const_val, size_t len) {
33543358
33553359static bool value_is_all_undef(ConstExprValue *const_val) {
33563360 switch (const_val->special) {
3361 case ConstValSpecialLazy:
3362 zig_unreachable();
33573363 case ConstValSpecialRuntime:
33583364 return false;
33593365 case ConstValSpecialUndef:
......@@ -5818,6 +5824,7 @@ static LLVMValueRef gen_const_ptr_union_recursive(CodeGen *g, ConstExprValue *un
58185824
58195825static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, ConstExprValue *const_val) {
58205826 switch (const_val->special) {
5827 case ConstValSpecialLazy:
58215828 case ConstValSpecialRuntime:
58225829 zig_unreachable();
58235830 case ConstValSpecialUndef:
......@@ -6075,6 +6082,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
60756082 assert(type_has_bits(type_entry));
60766083
60776084 switch (const_val->special) {
6085 case ConstValSpecialLazy:
60786086 case ConstValSpecialRuntime:
60796087 zig_unreachable();
60806088 case ConstValSpecialUndef:
......@@ -6834,9 +6842,9 @@ static void do_code_gen(CodeGen *g) {
68346842 fn_walk_var.data.vars.var = var;
68356843 iter_function_params_c_abi(g, fn_table_entry->type_entry, &fn_walk_var, var->src_arg_index);
68366844 } else {
6837 assert(var->gen_arg_index != SIZE_MAX);
68386845 ZigType *gen_type;
68396846 FnGenParamInfo *gen_info = &fn_table_entry->type_entry->data.fn.gen_param_info[var->src_arg_index];
6847 assert(gen_info->gen_index != SIZE_MAX);
68406848
68416849 if (handle_is_ptr(var->var_type)) {
68426850 if (gen_info->is_byval) {
......@@ -6844,7 +6852,7 @@ static void do_code_gen(CodeGen *g) {
68446852 } else {
68456853 gen_type = gen_info->type;
68466854 }
6847 var->value_ref = LLVMGetParam(fn, (unsigned)var->gen_arg_index);
6855 var->value_ref = LLVMGetParam(fn, gen_info->gen_index);
68486856 } else {
68496857 gen_type = var->var_type;
68506858 var->value_ref = build_alloca(g, var->var_type, buf_ptr(&var->name), var->align_bytes);
......@@ -6853,7 +6861,7 @@ static void do_code_gen(CodeGen *g) {
68536861 var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
68546862 buf_ptr(&var->name), import->data.structure.root_struct->di_file,
68556863 (unsigned)(var->decl_node->line + 1),
6856 get_llvm_di_type(g, gen_type), !g->strip_debug_symbols, 0, (unsigned)(var->gen_arg_index + 1));
6864 get_llvm_di_type(g, gen_type), !g->strip_debug_symbols, 0, (unsigned)(gen_info->gen_index));
68576865 }
68586866
68596867 }
......@@ -8241,7 +8249,7 @@ static void gen_root_source(CodeGen *g) {
82418249 }
82428250 Tld *panic_tld = find_decl(g, &get_container_scope(import_with_panic)->base, buf_create_from_str("panic"));
82438251 assert(panic_tld != nullptr);
8244 resolve_top_level_decl(g, panic_tld, nullptr);
8252 resolve_top_level_decl(g, panic_tld, nullptr, false);
82458253 }
82468254
82478255
src/ir.cpp+335-143
......@@ -154,6 +154,7 @@ struct ConstCastBadAllowsZero {
154154enum UndefAllowed {
155155 UndefOk,
156156 UndefBad,
157 LazyOk,
157158};
158159
159160static IrInstruction *ir_gen_node(IrBuilder *irb, AstNode *node, Scope *scope);
......@@ -10256,32 +10257,57 @@ static IrInstruction *ir_get_const_ptr(IrAnalyze *ira, IrInstruction *instructio
1025610257 return const_instr;
1025710258}
1025810259
10260static Error ir_resolve_const_val(CodeGen *codegen, IrExecutable *exec, AstNode *source_node,
10261 ConstExprValue *val, UndefAllowed undef_allowed)
10262{
10263 Error err;
10264 for (;;) {
10265 switch (val->special) {
10266 case ConstValSpecialStatic:
10267 return ErrorNone;
10268 case ConstValSpecialRuntime:
10269 if (!type_has_bits(val->type))
10270 return ErrorNone;
10271
10272 exec_add_error_node(codegen, exec, source_node,
10273 buf_sprintf("unable to evaluate constant expression"));
10274 return ErrorSemanticAnalyzeFail;
10275 case ConstValSpecialUndef:
10276 if (undef_allowed == UndefOk)
10277 return ErrorNone;
10278
10279 exec_add_error_node(codegen, exec, source_node,
10280 buf_sprintf("use of undefined value here causes undefined behavior"));
10281 return ErrorSemanticAnalyzeFail;
10282 case ConstValSpecialLazy:
10283 if (undef_allowed == LazyOk)
10284 return ErrorNone;
10285
10286 if ((err = ir_resolve_lazy(codegen, source_node, val)))
10287 return err;
10288
10289 continue;
10290 }
10291 }
10292}
10293
1025910294static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, UndefAllowed undef_allowed) {
10260 switch (value->value.special) {
10261 case ConstValSpecialStatic:
10262 return &value->value;
10263 case ConstValSpecialRuntime:
10264 if (!type_has_bits(value->value.type)) {
10265 return &value->value;
10266 }
10267 ir_add_error(ira, value, buf_sprintf("unable to evaluate constant expression"));
10268 return nullptr;
10269 case ConstValSpecialUndef:
10270 if (undef_allowed == UndefOk) {
10271 return &value->value;
10272 } else {
10273 ir_add_error(ira, value, buf_sprintf("use of undefined value here causes undefined behavior"));
10274 return nullptr;
10275 }
10295 Error err;
10296 if ((err = ir_resolve_const_val(ira->codegen, ira->new_irb.exec, value->source_node,
10297 &value->value, undef_allowed)))
10298 {
10299 return nullptr;
1027610300 }
10277 zig_unreachable();
10301 return &value->value;
1027810302}
1027910303
1028010304ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
1028110305 ZigType *expected_type, size_t *backward_branch_count, size_t backward_branch_quota,
1028210306 ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,
10283 IrExecutable *parent_exec, AstNode *expected_type_source_node)
10307 IrExecutable *parent_exec, AstNode *expected_type_source_node, bool allow_lazy)
1028410308{
10309 Error err;
10310
1028510311 if (expected_type != nullptr && type_is_invalid(expected_type))
1028610312 return &codegen->invalid_instruction->value;
1028710313
......@@ -10326,7 +10352,24 @@ ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *nod
1032610352 fprintf(stderr, "}\n");
1032710353 }
1032810354
10329 return ir_exec_const_result(codegen, analyzed_executable);
10355 ConstExprValue *result = ir_exec_const_result(codegen, analyzed_executable);
10356
10357 if (!allow_lazy) {
10358 if ((err = ir_resolve_lazy(codegen, node, result)))
10359 return &codegen->invalid_instruction->value;
10360 }
10361 return result;
10362}
10363
10364static ZigType *ir_resolve_const_type(CodeGen *codegen, IrExecutable *exec, AstNode *source_node,
10365 ConstExprValue *val)
10366{
10367 Error err;
10368 if ((err = ir_resolve_const_val(codegen, exec, source_node, val, UndefBad)))
10369 return codegen->builtin_types.entry_invalid;
10370
10371 assert(val->data.x_type != nullptr);
10372 return val->data.x_type;
1033010373}
1033110374
1033210375static ZigType *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {
......@@ -10339,12 +10382,7 @@ static ZigType *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {
1033910382 return ira->codegen->builtin_types.entry_invalid;
1034010383 }
1034110384
10342 ConstExprValue *const_val = ir_resolve_const(ira, type_value, UndefBad);
10343 if (!const_val)
10344 return ira->codegen->builtin_types.entry_invalid;
10345
10346 assert(const_val->data.x_type != nullptr);
10347 return const_val->data.x_type;
10385 return ir_resolve_const_type(ira->codegen, ira->new_irb.exec, type_value->source_node, &type_value->value);
1034810386}
1034910387
1035010388static ZigType *ir_resolve_error_set_type(IrAnalyze *ira, IrInstruction *op_source, IrInstruction *type_value) {
......@@ -11835,33 +11873,38 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc
1183511873 }
1183611874}
1183711875
11838static bool ir_resolve_align(IrAnalyze *ira, IrInstruction *value, uint32_t *out) {
11839 if (type_is_invalid(value->value.type))
11840 return false;
11841
11842 IrInstruction *casted_value = ir_implicit_cast(ira, value, get_align_amt_type(ira->codegen));
11843 if (type_is_invalid(casted_value->value.type))
11844 return false;
11845
11846 ConstExprValue *const_val = ir_resolve_const(ira, casted_value, UndefBad);
11847 if (!const_val)
11876static bool ir_resolve_const_align(CodeGen *codegen, IrExecutable *exec, AstNode *source_node,
11877 ConstExprValue *const_val, uint32_t *out)
11878{
11879 Error err;
11880 if ((err = ir_resolve_const_val(codegen, exec, source_node, const_val, UndefBad)))
1184811881 return false;
1184911882
1185011883 uint32_t align_bytes = bigint_as_unsigned(&const_val->data.x_bigint);
1185111884 if (align_bytes == 0) {
11852 ir_add_error(ira, value, buf_sprintf("alignment must be >= 1"));
11885 exec_add_error_node(codegen, exec, source_node, buf_sprintf("alignment must be >= 1"));
1185311886 return false;
1185411887 }
1185511888
1185611889 if (!is_power_of_2(align_bytes)) {
11857 ir_add_error(ira, value, buf_sprintf("alignment value %" PRIu32 " is not a power of 2", align_bytes));
11890 exec_add_error_node(codegen, exec, source_node, buf_sprintf("alignment value %" PRIu32 " is not a power of 2", align_bytes));
1185811891 return false;
1185911892 }
11860
1186111893 *out = align_bytes;
1186211894 return true;
1186311895}
1186411896
11897static bool ir_resolve_align(IrAnalyze *ira, IrInstruction *value, uint32_t *out) {
11898 if (type_is_invalid(value->value.type))
11899 return false;
11900
11901 IrInstruction *casted_value = ir_implicit_cast(ira, value, get_align_amt_type(ira->codegen));
11902 if (type_is_invalid(casted_value->value.type))
11903 return false;
11904
11905 return ir_resolve_const_align(ira->codegen, ira->new_irb.exec, value->source_node, &casted_value->value, out);
11906}
11907
1186511908static bool ir_resolve_unsigned(IrAnalyze *ira, IrInstruction *value, ZigType *int_type, uint64_t *out) {
1186611909 if (type_is_invalid(value->value.type))
1186711910 return false;
......@@ -12029,6 +12072,140 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) {
1202912072 return result;
1203012073}
1203112074
12075static ZigType *ir_resolve_lazy_fn_type(CodeGen *codegen, IrExecutable *exec, AstNode *source_node,
12076 LazyValueFnType *lazy_fn_type)
12077{
12078 AstNode *proto_node = lazy_fn_type->proto_node;
12079
12080 FnTypeId fn_type_id = {0};
12081 init_fn_type_id(&fn_type_id, proto_node, proto_node->data.fn_proto.params.length);
12082
12083 for (; fn_type_id.next_param_index < fn_type_id.param_count; fn_type_id.next_param_index += 1) {
12084 AstNode *param_node = proto_node->data.fn_proto.params.at(fn_type_id.next_param_index);
12085 assert(param_node->type == NodeTypeParamDecl);
12086
12087 bool param_is_var_args = param_node->data.param_decl.is_var_args;
12088 if (param_is_var_args) {
12089 if (fn_type_id.cc == CallingConventionC) {
12090 fn_type_id.param_count = fn_type_id.next_param_index;
12091 continue;
12092 } else if (fn_type_id.cc == CallingConventionUnspecified) {
12093 return get_generic_fn_type(codegen, &fn_type_id);
12094 } else {
12095 zig_unreachable();
12096 }
12097 }
12098 FnTypeParamInfo *param_info = &fn_type_id.param_info[fn_type_id.next_param_index];
12099 param_info->is_noalias = param_node->data.param_decl.is_noalias;
12100
12101 if (lazy_fn_type->param_types[fn_type_id.next_param_index] == nullptr) {
12102 param_info->type = nullptr;
12103 return get_generic_fn_type(codegen, &fn_type_id);
12104 } else {
12105 ZigType *param_type = ir_resolve_const_type(codegen, exec, source_node,
12106 lazy_fn_type->param_types[fn_type_id.next_param_index]);
12107 if (type_is_invalid(param_type))
12108 return nullptr;
12109 switch (type_requires_comptime(codegen, param_type)) {
12110 case ReqCompTimeYes:
12111 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
12112 exec_add_error_node(codegen, exec, source_node,
12113 buf_sprintf("parameter of type '%s' not allowed in function with calling convention '%s'",
12114 buf_ptr(&param_type->name), calling_convention_name(fn_type_id.cc)));
12115 return nullptr;
12116 }
12117 param_info->type = param_type;
12118 fn_type_id.next_param_index += 1;
12119 return get_generic_fn_type(codegen, &fn_type_id);
12120 case ReqCompTimeInvalid:
12121 return nullptr;
12122 case ReqCompTimeNo:
12123 break;
12124 }
12125 if (!type_has_bits(param_type) && !calling_convention_allows_zig_types(fn_type_id.cc)) {
12126 exec_add_error_node(codegen, exec, source_node,
12127 buf_sprintf("parameter of type '%s' has 0 bits; not allowed in function with calling convention '%s'",
12128 buf_ptr(&param_type->name), calling_convention_name(fn_type_id.cc)));
12129 return nullptr;
12130 }
12131 param_info->type = param_type;
12132 }
12133
12134 }
12135
12136 if (lazy_fn_type->align_val != nullptr) {
12137 if (!ir_resolve_const_align(codegen, exec, source_node, lazy_fn_type->align_val, &fn_type_id.alignment))
12138 return nullptr;
12139 }
12140
12141 fn_type_id.return_type = ir_resolve_const_type(codegen, exec, source_node, lazy_fn_type->return_type);
12142 if (type_is_invalid(fn_type_id.return_type))
12143 return nullptr;
12144 if (fn_type_id.return_type->id == ZigTypeIdOpaque) {
12145 exec_add_error_node(codegen, exec, source_node,
12146 buf_sprintf("return type cannot be opaque"));
12147 return nullptr;
12148 }
12149
12150 if (lazy_fn_type->async_allocator_type != nullptr) {
12151 fn_type_id.async_allocator_type = ir_resolve_const_type(codegen, exec, source_node,
12152 lazy_fn_type->async_allocator_type);
12153 if (type_is_invalid(fn_type_id.async_allocator_type))
12154 return nullptr;
12155 }
12156
12157 return get_fn_type(codegen, &fn_type_id);
12158}
12159
12160Error ir_resolve_lazy(CodeGen *codegen, AstNode *source_node, ConstExprValue *val) {
12161 Error err;
12162 if (val->special != ConstValSpecialLazy)
12163 return ErrorNone;
12164 IrExecutable *exec = val->data.x_lazy->exec;
12165 switch (val->data.x_lazy->id) {
12166 case LazyValueIdInvalid:
12167 zig_unreachable();
12168 case LazyValueIdAlignOf: {
12169 LazyValueAlignOf *lazy_align_of = reinterpret_cast<LazyValueAlignOf *>(val->data.x_lazy);
12170 if ((err = type_resolve(codegen, lazy_align_of->target_type, ResolveStatusAlignmentKnown)))
12171 return err;
12172 uint64_t align_in_bytes = get_abi_alignment(codegen, lazy_align_of->target_type);
12173 val->special = ConstValSpecialStatic;
12174 assert(val->type->id == ZigTypeIdComptimeInt);
12175 bigint_init_unsigned(&val->data.x_bigint, align_in_bytes);
12176 return ErrorNone;
12177 }
12178 case LazyValueIdSliceType: {
12179 LazyValueSliceType *lazy_slice_type = reinterpret_cast<LazyValueSliceType *>(val->data.x_lazy);
12180 uint32_t align_bytes = 0;
12181 if (lazy_slice_type->align_val != nullptr) {
12182 if (!ir_resolve_const_align(codegen, exec, source_node, lazy_slice_type->align_val, &align_bytes))
12183 return ErrorSemanticAnalyzeFail;
12184 }
12185 if ((err = type_resolve(codegen, lazy_slice_type->elem_type, ResolveStatusZeroBitsKnown)))
12186 return err;
12187 ZigType *slice_ptr_type = get_pointer_to_type_extra(codegen, lazy_slice_type->elem_type,
12188 lazy_slice_type->is_const, lazy_slice_type->is_volatile, PtrLenUnknown, align_bytes,
12189 0, 0, lazy_slice_type->is_allowzero);
12190 val->special = ConstValSpecialStatic;
12191 assert(val->type->id == ZigTypeIdMetaType);
12192 val->data.x_type = get_slice_type(codegen, slice_ptr_type);
12193 return ErrorNone;
12194 }
12195 case LazyValueIdFnType: {
12196 ZigType *fn_type = ir_resolve_lazy_fn_type(codegen, exec, source_node,
12197 reinterpret_cast<LazyValueFnType *>(val->data.x_lazy));
12198 if (fn_type == nullptr)
12199 return ErrorSemanticAnalyzeFail;
12200 val->special = ConstValSpecialStatic;
12201 assert(val->type->id == ZigTypeIdMetaType);
12202 val->data.x_type = fn_type;
12203 return ErrorNone;
12204 }
12205 }
12206 zig_unreachable();
12207}
12208
1203212209static IrInstruction *ir_analyze_instruction_add_implicit_return_type(IrAnalyze *ira,
1203312210 IrInstructionAddImplicitReturnType *instruction)
1203412211{
......@@ -13964,20 +14141,19 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
1396414141}
1396514142
1396614143static ZigVar *get_fn_var_by_index(ZigFn *fn_entry, size_t index) {
14144 FnTypeParamInfo *src_param_info = &fn_entry->type_entry->data.fn.fn_type_id.param_info[index];
14145 if (!type_has_bits(src_param_info->type))
14146 return nullptr;
14147
1396714148 size_t next_var_i = 0;
13968 FnGenParamInfo *gen_param_info = fn_entry->type_entry->data.fn.gen_param_info;
13969 assert(gen_param_info != nullptr);
1397014149 for (size_t param_i = 0; param_i < index; param_i += 1) {
13971 FnGenParamInfo *info = &gen_param_info[param_i];
13972 if (info->gen_index == SIZE_MAX)
14150 FnTypeParamInfo *src_param_info = &fn_entry->type_entry->data.fn.fn_type_id.param_info[param_i];
14151 if (!type_has_bits(src_param_info->type)) {
1397314152 continue;
14153 }
1397414154
1397514155 next_var_i += 1;
1397614156 }
13977 FnGenParamInfo *info = &gen_param_info[index];
13978 if (info->gen_index == SIZE_MAX)
13979 return nullptr;
13980
1398114157 return fn_entry->variable_list.at(next_var_i);
1398214158}
1398314159
......@@ -14003,7 +14179,7 @@ static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction,
1400314179 if (linkage_makes_it_runtime)
1400414180 goto no_mem_slot;
1400514181
14006 if (var->const_value->special == ConstValSpecialStatic) {
14182 if (value_is_comptime(var->const_value)) {
1400714183 mem_slot = var->const_value;
1400814184 } else {
1400914185 if (var->mem_slot_index != SIZE_MAX && (comptime_var_mem || var->gen_is_const)) {
......@@ -14021,6 +14197,7 @@ static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction,
1402114197 case ConstValSpecialRuntime:
1402214198 goto no_mem_slot;
1402314199 case ConstValSpecialStatic: // fallthrough
14200 case ConstValSpecialLazy: // fallthrough
1402414201 case ConstValSpecialUndef: {
1402514202 ConstPtrMut ptr_mut;
1402614203 if (comptime_var_mem) {
......@@ -14301,7 +14478,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call
1430114478 AstNode *body_node = fn_entry->body_node;
1430214479 result = ir_eval_const_value(ira->codegen, exec_scope, body_node, return_type,
1430314480 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, fn_entry,
14304 nullptr, call_instruction->base.source_node, nullptr, ira->new_irb.exec, return_type_node);
14481 nullptr, call_instruction->base.source_node, nullptr, ira->new_irb.exec, return_type_node, false);
1430514482
1430614483 if (inferred_err_set_type != nullptr) {
1430714484 inferred_err_set_type->data.error_set.infer_fn = nullptr;
......@@ -14497,7 +14674,8 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call
1449714674 ConstExprValue *align_result = ir_eval_const_value(ira->codegen, impl_fn->child_scope,
1449814675 fn_proto_node->data.fn_proto.align_expr, get_align_amt_type(ira->codegen),
1449914676 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota,
14500 nullptr, nullptr, fn_proto_node->data.fn_proto.align_expr, nullptr, ira->new_irb.exec, nullptr);
14677 nullptr, nullptr, fn_proto_node->data.fn_proto.align_expr, nullptr, ira->new_irb.exec, nullptr,
14678 false);
1450114679 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(&ira->new_irb,
1450214680 impl_fn->child_scope, fn_proto_node->data.fn_proto.align_expr);
1450314681 const_instruction->base.value = *align_result;
......@@ -15630,7 +15808,7 @@ static IrInstruction *ir_analyze_container_member_access_inner(IrAnalyze *ira,
1563015808 auto entry = container_scope->decl_table.maybe_get(field_name);
1563115809 Tld *tld = entry ? entry->value : nullptr;
1563215810 if (tld && tld->id == TldIdFn) {
15633 resolve_top_level_decl(ira->codegen, tld, source_instr->source_node);
15811 resolve_top_level_decl(ira->codegen, tld, source_instr->source_node, false);
1563415812 if (tld->resolution == TldResolutionInvalid)
1563515813 return ira->codegen->invalid_instruction;
1563615814 TldFn *tld_fn = (TldFn *)tld;
......@@ -15821,7 +15999,7 @@ static void add_link_lib_symbol(IrAnalyze *ira, Buf *lib_name, Buf *symbol_name,
1582115999
1582216000
1582316001static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_instruction, Tld *tld) {
15824 resolve_top_level_decl(ira->codegen, tld, source_instruction->source_node);
16002 resolve_top_level_decl(ira->codegen, tld, source_instruction->source_node, false);
1582516003 if (tld->resolution == TldResolutionInvalid)
1582616004 return ira->codegen->invalid_instruction;
1582716005
......@@ -16477,22 +16655,29 @@ static IrInstruction *ir_analyze_instruction_set_float_mode(IrAnalyze *ira,
1647716655static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1647816656 IrInstructionSliceType *slice_type_instruction)
1647916657{
16480 Error err;
16481 uint32_t align_bytes = 0;
16658 IrInstruction *result = ir_const(ira, &slice_type_instruction->base, ira->codegen->builtin_types.entry_type);
16659 result->value.special = ConstValSpecialLazy;
16660
16661 LazyValueSliceType *lazy_slice_type = allocate<LazyValueSliceType>(1);
16662 result->value.data.x_lazy = &lazy_slice_type->base;
16663 lazy_slice_type->base.id = LazyValueIdSliceType;
16664 lazy_slice_type->base.exec = ira->new_irb.exec;
16665
1648216666 if (slice_type_instruction->align_value != nullptr) {
16483 if (!ir_resolve_align(ira, slice_type_instruction->align_value->child, &align_bytes))
16667 lazy_slice_type->align_val = ir_resolve_const(ira, slice_type_instruction->align_value->child, LazyOk);
16668 if (lazy_slice_type->align_val == nullptr)
1648416669 return ira->codegen->invalid_instruction;
1648516670 }
1648616671
16487 ZigType *child_type = ir_resolve_type(ira, slice_type_instruction->child_type->child);
16488 if (type_is_invalid(child_type))
16672 lazy_slice_type->elem_type = ir_resolve_type(ira, slice_type_instruction->child_type->child);
16673 if (type_is_invalid(lazy_slice_type->elem_type))
1648916674 return ira->codegen->invalid_instruction;
1649016675
16491 bool is_const = slice_type_instruction->is_const;
16492 bool is_volatile = slice_type_instruction->is_volatile;
16493 bool is_allow_zero = slice_type_instruction->is_allow_zero;
16676 lazy_slice_type->is_const = slice_type_instruction->is_const;
16677 lazy_slice_type->is_volatile = slice_type_instruction->is_volatile;
16678 lazy_slice_type->is_allowzero = slice_type_instruction->is_allow_zero;
1649416679
16495 switch (child_type->id) {
16680 switch (lazy_slice_type->elem_type->id) {
1649616681 case ZigTypeIdInvalid: // handled above
1649716682 zig_unreachable();
1649816683 case ZigTypeIdUnreachable:
......@@ -16501,7 +16686,7 @@ static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1650116686 case ZigTypeIdArgTuple:
1650216687 case ZigTypeIdOpaque:
1650316688 ir_add_error_node(ira, slice_type_instruction->base.source_node,
16504 buf_sprintf("slice of type '%s' not allowed", buf_ptr(&child_type->name)));
16689 buf_sprintf("slice of type '%s' not allowed", buf_ptr(&lazy_slice_type->elem_type->name)));
1650516690 return ira->codegen->invalid_instruction;
1650616691 case ZigTypeIdMetaType:
1650716692 case ZigTypeIdVoid:
......@@ -16523,14 +16708,7 @@ static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1652316708 case ZigTypeIdBoundFn:
1652416709 case ZigTypeIdPromise:
1652516710 case ZigTypeIdVector:
16526 {
16527 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusZeroBitsKnown)))
16528 return ira->codegen->invalid_instruction;
16529 ZigType *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, child_type,
16530 is_const, is_volatile, PtrLenUnknown, align_bytes, 0, 0, is_allow_zero);
16531 ZigType *result_type = get_slice_type(ira->codegen, slice_ptr_type);
16532 return ir_const_type(ira, &slice_type_instruction->base, result_type);
16533 }
16711 return result;
1653416712 }
1653516713 zig_unreachable();
1653616714}
......@@ -16637,7 +16815,7 @@ static IrInstruction *ir_analyze_instruction_array_type(IrAnalyze *ira,
1663716815 case ZigTypeIdPromise:
1663816816 case ZigTypeIdVector:
1663916817 {
16640 if ((err = ensure_complete_type(ira->codegen, child_type)))
16818 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusSizeKnown)))
1664116819 return ira->codegen->invalid_instruction;
1664216820 ZigType *result_type = get_array_type(ira->codegen, child_type, size);
1664316821 return ir_const_type(ira, &array_type_instruction->base, result_type);
......@@ -17513,6 +17691,8 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc
1751317691static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
1751417692 IrInstructionContainerInitList *instruction)
1751517693{
17694 Error err;
17695
1751617696 ZigType *container_type = ir_resolve_type(ira, instruction->container_type->child);
1751717697 if (type_is_invalid(container_type))
1751817698 return ira->codegen->invalid_instruction;
......@@ -17541,6 +17721,10 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
1754117721 child_type = pointer_type->data.pointer.child_type;
1754217722 }
1754317723
17724 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusSizeKnown))) {
17725 return ira->codegen->invalid_instruction;
17726 }
17727
1754417728 ZigType *fixed_size_array_type = get_array_type(ira->codegen, child_type, elem_count);
1754517729
1754617730 ConstExprValue const_val = {};
......@@ -17923,10 +18107,11 @@ static ZigType *ir_type_info_get_type(IrAnalyze *ira, const char *type_name, Zig
1792318107 Error err;
1792418108 ConstExprValue *type_info_var = get_builtin_value(ira->codegen, "TypeInfo");
1792518109 assert(type_info_var->type->id == ZigTypeIdMetaType);
17926 assertNoError(ensure_complete_type(ira->codegen, type_info_var->data.x_type));
17927
1792818110 ZigType *type_info_type = type_info_var->data.x_type;
1792918111 assert(type_info_type->id == ZigTypeIdUnion);
18112 if ((err = type_resolve(ira->codegen, type_info_type, ResolveStatusSizeKnown))) {
18113 zig_unreachable();
18114 }
1793018115
1793118116 if (type_name == nullptr && root == nullptr)
1793218117 return type_info_type;
......@@ -17960,7 +18145,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,
1796018145{
1796118146 Error err;
1796218147 ZigType *type_info_definition_type = ir_type_info_get_type(ira, "Definition", nullptr);
17963 if ((err = ensure_complete_type(ira->codegen, type_info_definition_type)))
18148 if ((err = type_resolve(ira->codegen, type_info_definition_type, ResolveStatusSizeKnown)))
1796418149 return err;
1796518150
1796618151 ensure_field_index(type_info_definition_type, "name", 0);
......@@ -17987,7 +18172,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,
1798718172 while ((curr_entry = decl_it.next()) != nullptr) {
1798818173 // If the definition is unresolved, force it to be resolved again.
1798918174 if (curr_entry->value->resolution == TldResolutionUnresolved) {
17990 resolve_top_level_decl(ira->codegen, curr_entry->value, curr_entry->value->source_node);
18175 resolve_top_level_decl(ira->codegen, curr_entry->value, curr_entry->value->source_node, false);
1799118176 if (curr_entry->value->resolution != TldResolutionOk) {
1799218177 return ErrorSemanticAnalyzeFail;
1799318178 }
......@@ -18480,6 +18665,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
1848018665 ensure_field_index(result->type, "fields", 2);
1848118666
1848218667 ZigType *type_info_enum_field_type = ir_type_info_get_type(ira, "EnumField", nullptr);
18668 if ((err = type_resolve(ira->codegen, type_info_enum_field_type, ResolveStatusSizeKnown))) {
18669 zig_unreachable();
18670 }
1848318671 uint32_t enum_field_count = type_entry->data.enumeration.src_field_count;
1848418672
1848518673 ConstExprValue *enum_field_array = create_const_vals(1);
......@@ -18523,6 +18711,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
1852318711 result->data.x_optional = nullptr;
1852418712 break;
1852518713 }
18714 if ((err = type_resolve(ira->codegen, type_info_error_type, ResolveStatusSizeKnown))) {
18715 zig_unreachable();
18716 }
1852618717 ConstExprValue *slice_val = create_const_vals(1);
1852718718 result->data.x_optional = slice_val;
1852818719
......@@ -18619,6 +18810,8 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
1861918810 ensure_field_index(result->type, "fields", 2);
1862018811
1862118812 ZigType *type_info_union_field_type = ir_type_info_get_type(ira, "UnionField", nullptr);
18813 if ((err = type_resolve(ira->codegen, type_info_union_field_type, ResolveStatusSizeKnown)))
18814 zig_unreachable();
1862218815 uint32_t union_field_count = type_entry->data.unionation.src_field_count;
1862318816
1862418817 ConstExprValue *union_field_array = create_const_vals(1);
......@@ -18696,6 +18889,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
1869618889 ensure_field_index(result->type, "fields", 1);
1869718890
1869818891 ZigType *type_info_struct_field_type = ir_type_info_get_type(ira, "StructField", nullptr);
18892 if ((err = type_resolve(ira->codegen, type_info_struct_field_type, ResolveStatusSizeKnown))) {
18893 zig_unreachable();
18894 }
1869918895 uint32_t struct_field_count = type_entry->data.structure.src_field_count;
1870018896
1870118897 ConstExprValue *struct_field_array = create_const_vals(1);
......@@ -18803,6 +18999,9 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
1880318999 }
1880419000 // args: []TypeInfo.FnArg
1880519001 ZigType *type_info_fn_arg_type = ir_type_info_get_type(ira, "FnArg", nullptr);
19002 if ((err = type_resolve(ira->codegen, type_info_fn_arg_type, ResolveStatusSizeKnown))) {
19003 zig_unreachable();
19004 }
1880619005 size_t fn_arg_count = type_entry->data.fn.fn_type_id.param_count -
1880719006 (is_varargs && type_entry->data.fn.fn_type_id.cc != CallingConventionC);
1880819007
......@@ -18962,7 +19161,7 @@ static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruct
1896219161 ZigType *void_type = ira->codegen->builtin_types.entry_void;
1896319162 ConstExprValue *cimport_result = ir_eval_const_value(ira->codegen, &cimport_scope->base, block_node, void_type,
1896419163 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, nullptr,
18965 &cimport_scope->buf, block_node, nullptr, nullptr, nullptr);
19164 &cimport_scope->buf, block_node, nullptr, nullptr, nullptr, false);
1896619165 if (type_is_invalid(cimport_result->type))
1896719166 return ira->codegen->invalid_instruction;
1896819167
......@@ -20509,15 +20708,11 @@ static IrInstruction *ir_analyze_instruction_handle(IrAnalyze *ira, IrInstructio
2050920708}
2051020709
2051120710static IrInstruction *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstructionAlignOf *instruction) {
20512 Error err;
2051320711 IrInstruction *type_value = instruction->type_value->child;
2051420712 if (type_is_invalid(type_value->value.type))
2051520713 return ira->codegen->invalid_instruction;
2051620714 ZigType *type_entry = ir_resolve_type(ira, type_value);
2051720715
20518 if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusAlignmentKnown)))
20519 return ira->codegen->invalid_instruction;
20520
2052120716 switch (type_entry->id) {
2052220717 case ZigTypeIdInvalid:
2052320718 zig_unreachable();
......@@ -20549,12 +20744,25 @@ static IrInstruction *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstruct
2054920744 case ZigTypeIdUnion:
2055020745 case ZigTypeIdFn:
2055120746 case ZigTypeIdVector:
20552 {
20553 uint64_t align_in_bytes = get_abi_alignment(ira->codegen, type_entry);
20554 return ir_const_unsigned(ira, &instruction->base, align_in_bytes);
20555 }
20747 break;
2055620748 }
20557 zig_unreachable();
20749 if (type_is_resolved(type_entry, ResolveStatusAlignmentKnown)) {
20750 uint64_t align_in_bytes = get_abi_alignment(ira->codegen, type_entry);
20751 return ir_const_unsigned(ira, &instruction->base, align_in_bytes);
20752 }
20753 // Here we create a lazy value in order to avoid resolving the alignment of the type
20754 // immediately. This avoids false positive dependency loops such as:
20755 // const Node = struct {
20756 // field: []align(@alignOf(Node)) Node,
20757 // };
20758 LazyValueAlignOf *lazy_align_of = allocate<LazyValueAlignOf>(1);
20759 lazy_align_of->base.id = LazyValueIdAlignOf;
20760 lazy_align_of->base.exec = ira->new_irb.exec;
20761 lazy_align_of->target_type = type_entry;
20762 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_num_lit_int);
20763 result->value.special = ConstValSpecialLazy;
20764 result->value.data.x_lazy = &lazy_align_of->base;
20765 return result;
2055820766}
2055920767
2056020768static IrInstruction *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInstructionOverflowOp *instruction) {
......@@ -20809,96 +21017,77 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct
2080921017 AstNode *proto_node = instruction->base.source_node;
2081021018 assert(proto_node->type == NodeTypeFnProto);
2081121019
21020 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_type);
21021 result->value.special = ConstValSpecialLazy;
21022
21023 LazyValueFnType *lazy_fn_type = allocate<LazyValueFnType>(1);
21024 result->value.data.x_lazy = &lazy_fn_type->base;
21025 lazy_fn_type->base.id = LazyValueIdFnType;
21026 lazy_fn_type->base.exec = ira->new_irb.exec;
21027
2081221028 if (proto_node->data.fn_proto.auto_err_set) {
2081321029 ir_add_error(ira, &instruction->base,
2081421030 buf_sprintf("inferring error set of return type valid only for function definitions"));
2081521031 return ira->codegen->invalid_instruction;
2081621032 }
2081721033
20818 FnTypeId fn_type_id = {0};
20819 init_fn_type_id(&fn_type_id, proto_node, proto_node->data.fn_proto.params.length);
21034 size_t param_count = proto_node->data.fn_proto.params.length;
21035 lazy_fn_type->proto_node = proto_node;
21036 lazy_fn_type->param_types = allocate<ConstExprValue *>(param_count);
2082021037
20821 for (; fn_type_id.next_param_index < fn_type_id.param_count; fn_type_id.next_param_index += 1) {
20822 AstNode *param_node = proto_node->data.fn_proto.params.at(fn_type_id.next_param_index);
21038 for (size_t i = 0; i < param_count; i += 1) {
21039 AstNode *param_node = proto_node->data.fn_proto.params.at(i);
2082321040 assert(param_node->type == NodeTypeParamDecl);
2082421041
2082521042 bool param_is_var_args = param_node->data.param_decl.is_var_args;
21043 lazy_fn_type->is_var_args = true;
2082621044 if (param_is_var_args) {
20827 if (fn_type_id.cc == CallingConventionC) {
20828 fn_type_id.param_count = fn_type_id.next_param_index;
20829 continue;
20830 } else if (fn_type_id.cc == CallingConventionUnspecified) {
20831 return ir_const_type(ira, &instruction->base, get_generic_fn_type(ira->codegen, &fn_type_id));
21045 if (proto_node->data.fn_proto.cc == CallingConventionC) {
21046 break;
21047 } else if (proto_node->data.fn_proto.cc == CallingConventionUnspecified) {
21048 lazy_fn_type->is_generic = true;
21049 return result;
2083221050 } else {
2083321051 zig_unreachable();
2083421052 }
2083521053 }
20836 FnTypeParamInfo *param_info = &fn_type_id.param_info[fn_type_id.next_param_index];
20837 param_info->is_noalias = param_node->data.param_decl.is_noalias;
2083821054
20839 if (instruction->param_types[fn_type_id.next_param_index] == nullptr) {
20840 param_info->type = nullptr;
20841 return ir_const_type(ira, &instruction->base, get_generic_fn_type(ira->codegen, &fn_type_id));
20842 } else {
20843 IrInstruction *param_type_value = instruction->param_types[fn_type_id.next_param_index]->child;
20844 if (type_is_invalid(param_type_value->value.type))
20845 return ira->codegen->invalid_instruction;
20846 ZigType *param_type = ir_resolve_type(ira, param_type_value);
20847 switch (type_requires_comptime(ira->codegen, param_type)) {
20848 case ReqCompTimeYes:
20849 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
20850 ir_add_error(ira, param_type_value,
20851 buf_sprintf("parameter of type '%s' not allowed in function with calling convention '%s'",
20852 buf_ptr(&param_type->name), calling_convention_name(fn_type_id.cc)));
20853 return ira->codegen->invalid_instruction;
20854 }
20855 param_info->type = param_type;
20856 fn_type_id.next_param_index += 1;
20857 return ir_const_type(ira, &instruction->base, get_generic_fn_type(ira->codegen, &fn_type_id));
20858 case ReqCompTimeInvalid:
20859 return ira->codegen->invalid_instruction;
20860 case ReqCompTimeNo:
20861 break;
20862 }
20863 if (!type_has_bits(param_type) && !calling_convention_allows_zig_types(fn_type_id.cc)) {
20864 ir_add_error(ira, param_type_value,
20865 buf_sprintf("parameter of type '%s' has 0 bits; not allowed in function with calling convention '%s'",
20866 buf_ptr(&param_type->name), calling_convention_name(fn_type_id.cc)));
20867 return ira->codegen->invalid_instruction;
20868 }
20869 param_info->type = param_type;
21055 if (instruction->param_types[i] == nullptr) {
21056 lazy_fn_type->is_generic = true;
21057 return result;
2087021058 }
2087121059
21060 IrInstruction *param_type_value = instruction->param_types[i]->child;
21061 if (type_is_invalid(param_type_value->value.type))
21062 return ira->codegen->invalid_instruction;
21063 ConstExprValue *param_type_val = ir_resolve_const(ira, param_type_value, LazyOk);
21064 if (param_type_val == nullptr)
21065 return ira->codegen->invalid_instruction;
21066 lazy_fn_type->param_types[i] = param_type_val;
2087221067 }
2087321068
2087421069 if (instruction->align_value != nullptr) {
20875 if (!ir_resolve_align(ira, instruction->align_value->child, &fn_type_id.alignment))
21070 lazy_fn_type->align_val = ir_resolve_const(ira, instruction->align_value->child, LazyOk);
21071 if (lazy_fn_type->align_val == nullptr)
2087621072 return ira->codegen->invalid_instruction;
2087721073 }
2087821074
20879 IrInstruction *return_type_value = instruction->return_type->child;
20880 fn_type_id.return_type = ir_resolve_type(ira, return_type_value);
20881 if (type_is_invalid(fn_type_id.return_type))
20882 return ira->codegen->invalid_instruction;
20883 if (fn_type_id.return_type->id == ZigTypeIdOpaque) {
20884 ir_add_error(ira, instruction->return_type,
20885 buf_sprintf("return type cannot be opaque"));
21075 lazy_fn_type->return_type = ir_resolve_const(ira, instruction->return_type->child, LazyOk);
21076 if (lazy_fn_type->return_type == nullptr)
2088621077 return ira->codegen->invalid_instruction;
20887 }
2088821078
20889 if (fn_type_id.cc == CallingConventionAsync) {
21079 if (proto_node->data.fn_proto.cc == CallingConventionAsync) {
2089021080 if (instruction->async_allocator_type_value == nullptr) {
2089121081 ir_add_error(ira, &instruction->base,
2089221082 buf_sprintf("async fn proto missing allocator type"));
2089321083 return ira->codegen->invalid_instruction;
2089421084 }
20895 IrInstruction *async_allocator_type_value = instruction->async_allocator_type_value->child;
20896 fn_type_id.async_allocator_type = ir_resolve_type(ira, async_allocator_type_value);
20897 if (type_is_invalid(fn_type_id.async_allocator_type))
21085 lazy_fn_type->async_allocator_type = ir_resolve_const(ira, instruction->async_allocator_type_value->child, LazyOk);
21086 if (lazy_fn_type->async_allocator_type == nullptr)
2089821087 return ira->codegen->invalid_instruction;
2089921088 }
2090021089
20901 return ir_const_type(ira, &instruction->base, get_fn_type(ira->codegen, &fn_type_id));
21090 return result;
2090221091}
2090321092
2090421093static IrInstruction *ir_analyze_instruction_test_comptime(IrAnalyze *ira, IrInstructionTestComptime *instruction) {
......@@ -21567,8 +21756,11 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
2156721756 val->type->data.vector.len);
2156821757 case ZigTypeIdEnum:
2156921758 switch (val->type->data.enumeration.layout) {
21570 case ContainerLayoutAuto:
21571 zig_panic("TODO buf_read_value_bytes enum auto");
21759 case ContainerLayoutAuto: {
21760 opt_ir_add_error_node(ira, codegen, source_node,
21761 buf_sprintf("compiler bug: TODO: implement enum byte reinterpretation"));
21762 return ErrorSemanticAnalyzeFail;
21763 }
2157221764 case ContainerLayoutPacked:
2157321765 zig_panic("TODO buf_read_value_bytes enum packed");
2157421766 case ContainerLayoutExtern: {
......@@ -21864,7 +22056,7 @@ static IrInstruction *ir_analyze_instruction_decl_ref(IrAnalyze *ira,
2186422056 Tld *tld = instruction->tld;
2186522057 LVal lval = instruction->lval;
2186622058
21867 resolve_top_level_decl(ira->codegen, tld, instruction->base.source_node);
22059 resolve_top_level_decl(ira->codegen, tld, instruction->base.source_node, true);
2186822060 if (tld->resolution == TldResolutionInvalid)
2186922061 return ira->codegen->invalid_instruction;
2187022062
src/ir.hpp+2-1
......@@ -16,7 +16,8 @@ bool ir_gen_fn(CodeGen *g, ZigFn *fn_entry);
1616ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
1717 ZigType *expected_type, size_t *backward_branch_count, size_t backward_branch_quota,
1818 ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,
19 IrExecutable *parent_exec, AstNode *expected_type_source_node);
19 IrExecutable *parent_exec, AstNode *expected_type_source_node, bool allow_lazy);
20Error ir_resolve_lazy(CodeGen *codegen, AstNode *source_node, ConstExprValue *val);
2021
2122ZigType *ir_analyze(CodeGen *g, IrExecutable *old_executable, IrExecutable *new_executable,
2223 ZigType *expected_type, AstNode *expected_type_source_node);