authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-21 19:20:18-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-21 19:27:51-04:00
logefdbede7abf91d5fe2836d95987a83e95e9fcf8e
tree79c60d1fbcb793ad98a6efa6feab03fed118f7a2
parent81c441f8855d4c58f0b2ff86d3d007cf0bf395d3
signaturelock-open Commit is signed but in an unrecognized format.

breaking: remove field alignment kludge

This breaks behavior tests as well as compile error notes for generic function calls. However it introduces better circular dependency compile errors. The next step is to add Lazy Values to fix the regressions.

5 files changed, 201 insertions(+), 312 deletions(-)

src/all_types.hpp+10-19
...@@ -69,9 +69,9 @@ struct IrExecutable {...@@ -69,9 +69,9 @@ struct IrExecutable {
69 IrExecutable *source_exec;69 IrExecutable *source_exec;
70 IrAnalyze *analysis;70 IrAnalyze *analysis;
71 Scope *begin_scope;71 Scope *begin_scope;
72 ErrorMsg *first_err_trace_msg;
72 ZigList<Tld *> tld_list;73 ZigList<Tld *> tld_list;
7374
74 bool invalid;
75 bool is_inline;75 bool is_inline;
76 bool is_generic_instantiation;76 bool is_generic_instantiation;
77 bool need_err_code_spill;77 bool need_err_code_spill;
...@@ -1129,11 +1129,10 @@ struct ZigTypeStruct {...@@ -1129,11 +1129,10 @@ struct ZigTypeStruct {
1129 ResolveStatus resolve_status;1129 ResolveStatus resolve_status;
11301130
1131 bool is_slice;1131 bool is_slice;
1132 bool resolve_loop_flag; // set this flag temporarily to detect infinite loops
1133 bool reported_infinite_err;
1134 // whether any of the fields require comptime1132 // whether any of the fields require comptime
1135 // known after ResolveStatusZeroBitsKnown1133 // known after ResolveStatusZeroBitsKnown
1136 bool requires_comptime;1134 bool requires_comptime;
1135 bool resolve_loop_flag;
1137};1136};
11381137
1139struct ZigTypeOptional {1138struct ZigTypeOptional {
...@@ -1155,26 +1154,20 @@ struct ZigTypeErrorSet {...@@ -1155,26 +1154,20 @@ struct ZigTypeErrorSet {
11551154
1156struct ZigTypeEnum {1155struct ZigTypeEnum {
1157 AstNode *decl_node;1156 AstNode *decl_node;
1158 ContainerLayout layout;
1159 uint32_t src_field_count;
1160 TypeEnumField *fields;1157 TypeEnumField *fields;
1161 bool is_invalid; // true if any fields are invalid
1162 ZigType *tag_int_type;1158 ZigType *tag_int_type;
11631159
1164 ScopeDecls *decls_scope;1160 ScopeDecls *decls_scope;
11651161
1166 // set this flag temporarily to detect infinite loops
1167 bool embedded_in_current;
1168 bool reported_infinite_err;
1169 // whether we've finished resolving it
1170 bool complete;
1171
1172 bool zero_bits_loop_flag;
1173 bool zero_bits_known;
1174
1175 LLVMValueRef name_function;1162 LLVMValueRef name_function;
11761163
1177 HashMap<Buf *, TypeEnumField *, buf_hash, buf_eql_buf> fields_by_name;1164 HashMap<Buf *, TypeEnumField *, buf_hash, buf_eql_buf> fields_by_name;
1165 uint32_t src_field_count;
1166
1167 ContainerLayout layout;
1168 ResolveStatus resolve_status;
1169
1170 bool resolve_loop_flag;
1178};1171};
11791172
1180uint32_t type_ptr_hash(const ZigType *ptr);1173uint32_t type_ptr_hash(const ZigType *ptr);
...@@ -1199,11 +1192,10 @@ struct ZigTypeUnion {...@@ -1199,11 +1192,10 @@ struct ZigTypeUnion {
1199 ResolveStatus resolve_status;1192 ResolveStatus resolve_status;
12001193
1201 bool have_explicit_tag_type;1194 bool have_explicit_tag_type;
1202 bool resolve_loop_flag; // set this flag temporarily to detect infinite loops
1203 bool reported_infinite_err;
1204 // whether any of the fields require comptime1195 // whether any of the fields require comptime
1205 // the value is not valid until zero_bits_known == true1196 // the value is not valid until zero_bits_known == true
1206 bool requires_comptime;1197 bool requires_comptime;
1198 bool resolve_loop_flag;
1207};1199};
12081200
1209struct FnGenParamInfo {1201struct FnGenParamInfo {
...@@ -1715,6 +1707,7 @@ struct CodeGen {...@@ -1715,6 +1707,7 @@ struct CodeGen {
1715 //////////////////////////// Runtime State1707 //////////////////////////// Runtime State
1716 LLVMModuleRef module;1708 LLVMModuleRef module;
1717 ZigList<ErrorMsg*> errors;1709 ZigList<ErrorMsg*> errors;
1710 ErrorMsg *trace_err;
1718 LLVMBuilderRef builder;1711 LLVMBuilderRef builder;
1719 ZigLLVMDIBuilder *dbuilder;1712 ZigLLVMDIBuilder *dbuilder;
1720 ZigLLVMDICompileUnit *compile_unit;1713 ZigLLVMDICompileUnit *compile_unit;
...@@ -1767,7 +1760,6 @@ struct CodeGen {...@@ -1767,7 +1760,6 @@ struct CodeGen {
1767 ZigList<Tld *> resolve_queue;1760 ZigList<Tld *> resolve_queue;
1768 size_t resolve_queue_index;1761 size_t resolve_queue_index;
1769 ZigList<TimeEvent> timing_events;1762 ZigList<TimeEvent> timing_events;
1770 ZigList<AstNode *> tld_ref_source_node_stack;
1771 ZigList<ZigFn *> inline_fns;1763 ZigList<ZigFn *> inline_fns;
1772 ZigList<ZigFn *> test_fns;1764 ZigList<ZigFn *> test_fns;
1773 ZigList<ErrorTableEntry *> errors_by_index;1765 ZigList<ErrorTableEntry *> errors_by_index;
...@@ -1852,7 +1844,6 @@ struct CodeGen {...@@ -1852,7 +1844,6 @@ struct CodeGen {
1852 ZigFn *main_fn;1844 ZigFn *main_fn;
1853 ZigFn *panic_fn;1845 ZigFn *panic_fn;
1854 TldFn *panic_tld_fn;1846 TldFn *panic_tld_fn;
1855 AstNode *root_export_decl;
18561847
1857 WantPIC want_pic;1848 WantPIC want_pic;
1858 WantStackCheck want_stack_check;1849 WantStackCheck want_stack_check;
src/analyze.cpp+118-225
...@@ -20,7 +20,7 @@...@@ -20,7 +20,7 @@
2020
21static const size_t default_backward_branch_quota = 1000;21static const size_t default_backward_branch_quota = 1000;
2222
23static Error resolve_struct_type(CodeGen *g, ZigType *struct_type);23static Error ATTRIBUTE_MUST_USE resolve_struct_type(CodeGen *g, ZigType *struct_type);
2424
25static Error ATTRIBUTE_MUST_USE resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type);25static Error ATTRIBUTE_MUST_USE resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type);
26static Error ATTRIBUTE_MUST_USE resolve_struct_alignment(CodeGen *g, ZigType *struct_type);26static Error ATTRIBUTE_MUST_USE resolve_struct_alignment(CodeGen *g, ZigType *struct_type);
...@@ -271,6 +271,8 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {...@@ -271,6 +271,8 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
271 return type_entry->data.structure.resolve_status >= status;271 return type_entry->data.structure.resolve_status >= status;
272 case ZigTypeIdUnion:272 case ZigTypeIdUnion:
273 return type_entry->data.unionation.resolve_status >= status;273 return type_entry->data.unionation.resolve_status >= status;
274 case ZigTypeIdEnum:
275 return type_entry->data.enumeration.resolve_status >= status;
274 case ZigTypeIdFnFrame:276 case ZigTypeIdFnFrame:
275 switch (status) {277 switch (status) {
276 case ResolveStatusInvalid:278 case ResolveStatusInvalid:
...@@ -285,23 +287,6 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {...@@ -285,23 +287,6 @@ bool type_is_resolved(ZigType *type_entry, ResolveStatus status) {
285 case ResolveStatusLLVMFull:287 case ResolveStatusLLVMFull:
286 return type_entry->llvm_type != nullptr;288 return type_entry->llvm_type != nullptr;
287 }289 }
288 case ZigTypeIdEnum:
289 switch (status) {
290 case ResolveStatusUnstarted:
291 return true;
292 case ResolveStatusInvalid:
293 zig_unreachable();
294 case ResolveStatusZeroBitsKnown:
295 return type_entry->data.enumeration.zero_bits_known;
296 case ResolveStatusAlignmentKnown:
297 return type_entry->data.enumeration.zero_bits_known;
298 case ResolveStatusSizeKnown:
299 return type_entry->data.enumeration.complete;
300 case ResolveStatusLLVMFwdDecl:
301 case ResolveStatusLLVMFull:
302 return type_entry->llvm_di_type != nullptr;
303 }
304 zig_unreachable();
305 case ZigTypeIdOpaque:290 case ZigTypeIdOpaque:
306 return status < ResolveStatusSizeKnown;291 return status < ResolveStatusSizeKnown;
307 case ZigTypeIdMetaType:292 case ZigTypeIdMetaType:
...@@ -865,7 +850,7 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {...@@ -865,7 +850,7 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
865 return table_entry->value;850 return table_entry->value;
866 }851 }
867 if (fn_type_id->return_type != nullptr) {852 if (fn_type_id->return_type != nullptr) {
868 if ((err = ensure_complete_type(g, fn_type_id->return_type)))853 if ((err = type_resolve(g, fn_type_id->return_type, ResolveStatusSizeKnown)))
869 return g->builtin_types.entry_invalid;854 return g->builtin_types.entry_invalid;
870 assert(fn_type_id->return_type->id != ZigTypeIdOpaque);855 assert(fn_type_id->return_type->id != ZigTypeIdOpaque);
871 } else {856 } else {
...@@ -1404,7 +1389,6 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc...@@ -1404,7 +1389,6 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
1404 add_node_error(g, proto_node,1389 add_node_error(g, proto_node,
1405 buf_sprintf("TODO implement inferred return types https://github.com/ziglang/zig/issues/447"));1390 buf_sprintf("TODO implement inferred return types https://github.com/ziglang/zig/issues/447"));
1406 return g->builtin_types.entry_invalid;1391 return g->builtin_types.entry_invalid;
1407 //return get_generic_fn_type(g, &fn_type_id);
1408 }1392 }
14091393
1410 ZigType *specified_return_type = analyze_type_expr(g, child_scope, fn_proto->return_type);1394 ZigType *specified_return_type = analyze_type_expr(g, child_scope, fn_proto->return_type);
...@@ -1490,7 +1474,7 @@ bool type_is_invalid(ZigType *type_entry) {...@@ -1490,7 +1474,7 @@ bool type_is_invalid(ZigType *type_entry) {
1490 case ZigTypeIdUnion:1474 case ZigTypeIdUnion:
1491 return type_entry->data.unionation.resolve_status == ResolveStatusInvalid;1475 return type_entry->data.unionation.resolve_status == ResolveStatusInvalid;
1492 case ZigTypeIdEnum:1476 case ZigTypeIdEnum:
1493 return type_entry->data.enumeration.is_invalid;1477 return type_entry->data.enumeration.resolve_status == ResolveStatusInvalid;
1494 default:1478 default:
1495 return false;1479 return false;
1496 }1480 }
...@@ -1602,9 +1586,8 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {...@@ -1602,9 +1586,8 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
1602 if (struct_type->data.structure.resolve_loop_flag) {1586 if (struct_type->data.structure.resolve_loop_flag) {
1603 if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) {1587 if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) {
1604 struct_type->data.structure.resolve_status = ResolveStatusInvalid;1588 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
1605 ErrorMsg *msg = add_node_error(g, decl_node,1589 g->trace_err = add_node_error(g, decl_node,
1606 buf_sprintf("struct '%s' contains itself", buf_ptr(&struct_type->name)));1590 buf_sprintf("struct '%s' depends on its own size", buf_ptr(&struct_type->name)));
1607 emit_error_notes_for_ref_stack(g, msg);
1608 }1591 }
1609 return ErrorSemanticAnalyzeFail;1592 return ErrorSemanticAnalyzeFail;
1610 }1593 }
...@@ -1728,14 +1711,13 @@ static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) {...@@ -1728,14 +1711,13 @@ static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) {
1728 if (union_type->data.unionation.resolve_status >= ResolveStatusAlignmentKnown)1711 if (union_type->data.unionation.resolve_status >= ResolveStatusAlignmentKnown)
1729 return ErrorNone;1712 return ErrorNone;
17301713
1714 AstNode *decl_node = union_type->data.structure.decl_node;
1715
1731 if (union_type->data.unionation.resolve_loop_flag) {1716 if (union_type->data.unionation.resolve_loop_flag) {
1732 if (!union_type->data.unionation.reported_infinite_err) {1717 if (union_type->data.unionation.resolve_status != ResolveStatusInvalid) {
1733 AstNode *decl_node = union_type->data.unionation.decl_node;
1734 union_type->data.unionation.reported_infinite_err = true;
1735 union_type->data.unionation.resolve_status = ResolveStatusInvalid;1718 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
1736 ErrorMsg *msg = add_node_error(g, decl_node,1719 g->trace_err = add_node_error(g, decl_node,
1737 buf_sprintf("union '%s' contains itself", buf_ptr(&union_type->name)));1720 buf_sprintf("union '%s' depends on its own alignment", buf_ptr(&union_type->name)));
1738 emit_error_notes_for_ref_stack(g, msg);
1739 }1721 }
1740 return ErrorSemanticAnalyzeFail;1722 return ErrorSemanticAnalyzeFail;
1741 }1723 }
...@@ -1752,13 +1734,12 @@ static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) {...@@ -1752,13 +1734,12 @@ static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) {
1752 if (field->gen_index == UINT32_MAX)1734 if (field->gen_index == UINT32_MAX)
1753 continue;1735 continue;
17541736
1737 src_assert(field->type_entry != nullptr, decl_node);
1738
1755 size_t this_field_align;1739 size_t this_field_align;
1756 if (packed) {1740 if (packed) {
1757 // TODO: https://github.com/ziglang/zig/issues/15121741 // TODO: https://github.com/ziglang/zig/issues/1512
1758 this_field_align = 1;1742 this_field_align = 1;
1759 // This is the same hack as resolve_struct_alignment. See the comment there.
1760 } else if (field->type_entry == nullptr) {
1761 this_field_align = g->builtin_types.entry_usize->abi_align;
1762 } else {1743 } else {
1763 if ((err = type_resolve(g, field->type_entry, ResolveStatusAlignmentKnown))) {1744 if ((err = type_resolve(g, field->type_entry, ResolveStatusAlignmentKnown))) {
1764 union_type->data.unionation.resolve_status = ResolveStatusInvalid;1745 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
...@@ -1839,12 +1820,10 @@ static Error resolve_union_type(CodeGen *g, ZigType *union_type) {...@@ -1839,12 +1820,10 @@ static Error resolve_union_type(CodeGen *g, ZigType *union_type) {
1839 size_t union_size_in_bits = 0;1820 size_t union_size_in_bits = 0;
18401821
1841 if (union_type->data.unionation.resolve_loop_flag) {1822 if (union_type->data.unionation.resolve_loop_flag) {
1842 if (!union_type->data.unionation.reported_infinite_err) {1823 if (union_type->data.unionation.resolve_status != ResolveStatusInvalid) {
1843 union_type->data.unionation.reported_infinite_err = true;
1844 union_type->data.unionation.resolve_status = ResolveStatusInvalid;1824 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
1845 ErrorMsg *msg = add_node_error(g, decl_node,1825 g->trace_err = add_node_error(g, decl_node,
1846 buf_sprintf("union '%s' depends on its own size", buf_ptr(&union_type->name)));1826 buf_sprintf("union '%s' depends on its own size", buf_ptr(&union_type->name)));
1847 emit_error_notes_for_ref_stack(g, msg);
1848 }1827 }
1849 return ErrorSemanticAnalyzeFail;1828 return ErrorSemanticAnalyzeFail;
1850 }1829 }
...@@ -1925,24 +1904,25 @@ static bool type_is_valid_extern_enum_tag(CodeGen *g, ZigType *ty) {...@@ -1925,24 +1904,25 @@ static bool type_is_valid_extern_enum_tag(CodeGen *g, ZigType *ty) {
1925static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {1904static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
1926 assert(enum_type->id == ZigTypeIdEnum);1905 assert(enum_type->id == ZigTypeIdEnum);
19271906
1928 if (enum_type->data.enumeration.is_invalid)1907 if (enum_type->data.enumeration.resolve_status == ResolveStatusInvalid)
1929 return ErrorSemanticAnalyzeFail;1908 return ErrorSemanticAnalyzeFail;
19301909 if (enum_type->data.enumeration.resolve_status >= ResolveStatusZeroBitsKnown)
1931 if (enum_type->data.enumeration.zero_bits_known)
1932 return ErrorNone;1910 return ErrorNone;
19331911
1934 if (enum_type->data.enumeration.zero_bits_loop_flag) {1912 AstNode *decl_node = enum_type->data.enumeration.decl_node;
1935 ErrorMsg *msg = add_node_error(g, enum_type->data.enumeration.decl_node,1913 assert(decl_node->type == NodeTypeContainerDecl);
1936 buf_sprintf("'%s' depends on itself", buf_ptr(&enum_type->name)));1914
1937 emit_error_notes_for_ref_stack(g, msg);1915 if (enum_type->data.enumeration.resolve_loop_flag) {
1938 enum_type->data.enumeration.is_invalid = true;1916 if (enum_type->data.enumeration.resolve_status != ResolveStatusInvalid) {
1917 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
1918 g->trace_err = add_node_error(g, decl_node,
1919 buf_sprintf("circular dependency: whether enum '%s' has non-zero size",
1920 buf_ptr(&enum_type->name)));
1921 }
1939 return ErrorSemanticAnalyzeFail;1922 return ErrorSemanticAnalyzeFail;
1940 }1923 }
19411924
1942 enum_type->data.enumeration.zero_bits_loop_flag = true;1925 enum_type->data.enumeration.resolve_loop_flag = true;
1943
1944 AstNode *decl_node = enum_type->data.enumeration.decl_node;
1945 assert(decl_node->type == NodeTypeContainerDecl);
19461926
1947 assert(!enum_type->data.enumeration.fields);1927 assert(!enum_type->data.enumeration.fields);
1948 uint32_t field_count = (uint32_t)decl_node->data.container_decl.fields.length;1928 uint32_t field_count = (uint32_t)decl_node->data.container_decl.fields.length;
...@@ -1951,9 +1931,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {...@@ -1951,9 +1931,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
19511931
1952 enum_type->data.enumeration.src_field_count = field_count;1932 enum_type->data.enumeration.src_field_count = field_count;
1953 enum_type->data.enumeration.fields = nullptr;1933 enum_type->data.enumeration.fields = nullptr;
1954 enum_type->data.enumeration.is_invalid = true;1934 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
1955 enum_type->data.enumeration.zero_bits_loop_flag = false;
1956 enum_type->data.enumeration.zero_bits_known = true;
1957 return ErrorSemanticAnalyzeFail;1935 return ErrorSemanticAnalyzeFail;
1958 }1936 }
19591937
...@@ -1982,14 +1960,14 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {...@@ -1982,14 +1960,14 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
1982 if (decl_node->data.container_decl.init_arg_expr != nullptr) {1960 if (decl_node->data.container_decl.init_arg_expr != nullptr) {
1983 ZigType *wanted_tag_int_type = analyze_type_expr(g, scope, decl_node->data.container_decl.init_arg_expr);1961 ZigType *wanted_tag_int_type = analyze_type_expr(g, scope, decl_node->data.container_decl.init_arg_expr);
1984 if (type_is_invalid(wanted_tag_int_type)) {1962 if (type_is_invalid(wanted_tag_int_type)) {
1985 enum_type->data.enumeration.is_invalid = true;1963 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
1986 } else if (wanted_tag_int_type->id != ZigTypeIdInt) {1964 } else if (wanted_tag_int_type->id != ZigTypeIdInt) {
1987 enum_type->data.enumeration.is_invalid = true;1965 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
1988 add_node_error(g, decl_node->data.container_decl.init_arg_expr,1966 add_node_error(g, decl_node->data.container_decl.init_arg_expr,
1989 buf_sprintf("expected integer, found '%s'", buf_ptr(&wanted_tag_int_type->name)));1967 buf_sprintf("expected integer, found '%s'", buf_ptr(&wanted_tag_int_type->name)));
1990 } else if (enum_type->data.enumeration.layout == ContainerLayoutExtern &&1968 } else if (enum_type->data.enumeration.layout == ContainerLayoutExtern &&
1991 !type_is_valid_extern_enum_tag(g, wanted_tag_int_type)) {1969 !type_is_valid_extern_enum_tag(g, wanted_tag_int_type)) {
1992 enum_type->data.enumeration.is_invalid = true;1970 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
1993 ErrorMsg *msg = add_node_error(g, decl_node->data.container_decl.init_arg_expr,1971 ErrorMsg *msg = add_node_error(g, decl_node->data.container_decl.init_arg_expr,
1994 buf_sprintf("'%s' is not a valid tag type for an extern enum",1972 buf_sprintf("'%s' is not a valid tag type for an extern enum",
1995 buf_ptr(&wanted_tag_int_type->name)));1973 buf_ptr(&wanted_tag_int_type->name)));
...@@ -2029,7 +2007,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {...@@ -2029,7 +2007,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
2029 ErrorMsg *msg = add_node_error(g, field_node,2007 ErrorMsg *msg = add_node_error(g, field_node,
2030 buf_sprintf("duplicate enum field: '%s'", buf_ptr(type_enum_field->name)));2008 buf_sprintf("duplicate enum field: '%s'", buf_ptr(type_enum_field->name)));
2031 add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here"));2009 add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here"));
2032 enum_type->data.enumeration.is_invalid = true;2010 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
2033 continue;2011 continue;
2034 }2012 }
20352013
...@@ -2039,7 +2017,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {...@@ -2039,7 +2017,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
2039 // A user-specified value is available2017 // A user-specified value is available
2040 ConstExprValue *result = analyze_const_value(g, scope, tag_value, tag_int_type, nullptr);2018 ConstExprValue *result = analyze_const_value(g, scope, tag_value, tag_int_type, nullptr);
2041 if (type_is_invalid(result->type)) {2019 if (type_is_invalid(result->type)) {
2042 enum_type->data.enumeration.is_invalid = true;2020 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
2043 continue;2021 continue;
2044 }2022 }
20452023
...@@ -2060,7 +2038,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {...@@ -2060,7 +2038,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
2060 if (!bigint_fits_in_bits(&type_enum_field->value,2038 if (!bigint_fits_in_bits(&type_enum_field->value,
2061 tag_int_type->size_in_bits,2039 tag_int_type->size_in_bits,
2062 tag_int_type->data.integral.is_signed)) {2040 tag_int_type->data.integral.is_signed)) {
2063 enum_type->data.enumeration.is_invalid = true;2041 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
20642042
2065 Buf *val_buf = buf_alloc();2043 Buf *val_buf = buf_alloc();
2066 bigint_append_buf(val_buf, &type_enum_field->value, 10);2044 bigint_append_buf(val_buf, &type_enum_field->value, 10);
...@@ -2075,7 +2053,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {...@@ -2075,7 +2053,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
2075 // Make sure the value is unique2053 // Make sure the value is unique
2076 auto entry = occupied_tag_values.put_unique(type_enum_field->value, field_node);2054 auto entry = occupied_tag_values.put_unique(type_enum_field->value, field_node);
2077 if (entry != nullptr) {2055 if (entry != nullptr) {
2078 enum_type->data.enumeration.is_invalid = true;2056 enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
20792057
2080 Buf *val_buf = buf_alloc();2058 Buf *val_buf = buf_alloc();
2081 bigint_append_buf(val_buf, &type_enum_field->value, 10);2059 bigint_append_buf(val_buf, &type_enum_field->value, 10);
...@@ -2089,13 +2067,12 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {...@@ -2089,13 +2067,12 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
2089 last_enum_field = type_enum_field;2067 last_enum_field = type_enum_field;
2090 }2068 }
20912069
2092 enum_type->data.enumeration.zero_bits_loop_flag = false;2070 if (enum_type->data.enumeration.resolve_status == ResolveStatusInvalid)
2093 enum_type->data.enumeration.zero_bits_known = true;
2094 enum_type->data.enumeration.complete = true;
2095
2096 if (enum_type->data.enumeration.is_invalid)
2097 return ErrorSemanticAnalyzeFail;2071 return ErrorSemanticAnalyzeFail;
20982072
2073 enum_type->data.enumeration.resolve_loop_flag = false;
2074 enum_type->data.enumeration.resolve_status = ResolveStatusSizeKnown;
2075
2099 return ErrorNone;2076 return ErrorNone;
2100}2077}
21012078
...@@ -2113,12 +2090,13 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {...@@ -2113,12 +2090,13 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
2113 assert(decl_node->type == NodeTypeContainerDecl);2090 assert(decl_node->type == NodeTypeContainerDecl);
21142091
2115 if (struct_type->data.structure.resolve_loop_flag) {2092 if (struct_type->data.structure.resolve_loop_flag) {
2116 // TODO This is a problem. I believe it can be solved with lazy values.2093 if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) {
2117 struct_type->size_in_bits = SIZE_MAX;2094 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2118 struct_type->abi_size = SIZE_MAX;2095 g->trace_err = add_node_error(g, decl_node,
2119 struct_type->data.structure.resolve_status = ResolveStatusZeroBitsKnown;2096 buf_sprintf("circular dependency: whether struct '%s' has non-zero size",
2120 struct_type->data.structure.resolve_loop_flag = false;2097 buf_ptr(&struct_type->name)));
2121 return ErrorNone;2098 }
2099 return ErrorSemanticAnalyzeFail;
2122 }2100 }
21232101
2124 struct_type->data.structure.resolve_loop_flag = true;2102 struct_type->data.structure.resolve_loop_flag = true;
...@@ -2237,9 +2215,8 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {...@@ -2237,9 +2215,8 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
2237 if (struct_type->data.structure.resolve_loop_flag) {2215 if (struct_type->data.structure.resolve_loop_flag) {
2238 if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) {2216 if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) {
2239 struct_type->data.structure.resolve_status = ResolveStatusInvalid;2217 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2240 ErrorMsg *msg = add_node_error(g, decl_node,2218 g->trace_err = add_node_error(g, decl_node,
2241 buf_sprintf("struct '%s' contains itself", buf_ptr(&struct_type->name)));2219 buf_sprintf("struct '%s' depends on its own alignment", buf_ptr(&struct_type->name)));
2242 emit_error_notes_for_ref_stack(g, msg);
2243 }2220 }
2244 return ErrorSemanticAnalyzeFail;2221 return ErrorSemanticAnalyzeFail;
2245 }2222 }
...@@ -2255,20 +2232,12 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {...@@ -2255,20 +2232,12 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
2255 if (field->gen_index == SIZE_MAX)2232 if (field->gen_index == SIZE_MAX)
2256 continue;2233 continue;
22572234
2235 src_assert(field->type_entry != nullptr, decl_node);
2236
2258 size_t this_field_align;2237 size_t this_field_align;
2259 if (packed) {2238 if (packed) {
2260 // TODO: https://github.com/ziglang/zig/issues/15122239 // TODO: https://github.com/ziglang/zig/issues/1512
2261 this_field_align = 1;2240 this_field_align = 1;
2262 // TODO If we have no type_entry for the field, we've already failed to
2263 // compile the program correctly. This stage1 compiler needs a deeper
2264 // reworking to make this correct, or we can ignore the problem
2265 // and make sure it is fixed in stage2. This workaround is for when
2266 // there is a false positive of a dependency loop, of alignment depending
2267 // on itself. When this false positive happens we assume a pointer-aligned
2268 // field, which is usually fine but could be incorrectly over-aligned or
2269 // even under-aligned. See https://github.com/ziglang/zig/issues/1512
2270 } else if (field->type_entry == nullptr) {
2271 this_field_align = g->builtin_types.entry_usize->abi_align;
2272 } else {2241 } else {
2273 if ((err = type_resolve(g, field->type_entry, ResolveStatusAlignmentKnown))) {2242 if ((err = type_resolve(g, field->type_entry, ResolveStatusAlignmentKnown))) {
2274 struct_type->data.structure.resolve_status = ResolveStatusInvalid;2243 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
...@@ -2304,23 +2273,21 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -2304,23 +2273,21 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
2304 if (union_type->data.unionation.resolve_status >= ResolveStatusZeroBitsKnown)2273 if (union_type->data.unionation.resolve_status >= ResolveStatusZeroBitsKnown)
2305 return ErrorNone;2274 return ErrorNone;
23062275
2276 AstNode *decl_node = union_type->data.unionation.decl_node;
2277 assert(decl_node->type == NodeTypeContainerDecl);
2278
2307 if (union_type->data.unionation.resolve_loop_flag) {2279 if (union_type->data.unionation.resolve_loop_flag) {
2308 // If we get here it's due to recursion. From this we conclude that the struct is2280 if (union_type->data.unionation.resolve_status != ResolveStatusInvalid) {
2309 // not zero bits.2281 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
2310 // TODO actually it could still be zero bits. Here we should continue analyzing2282 g->trace_err = add_node_error(g, decl_node,
2311 // the union from the next field index.2283 buf_sprintf("circular dependency: whether union '%s' has non-zero size",
2312 union_type->data.unionation.resolve_status = ResolveStatusZeroBitsKnown;2284 buf_ptr(&union_type->name)));
2313 union_type->data.unionation.resolve_loop_flag = false;2285 }
2314 union_type->abi_size = SIZE_MAX;2286 return ErrorSemanticAnalyzeFail;
2315 union_type->size_in_bits = SIZE_MAX;
2316 return ErrorNone;
2317 }2287 }
23182288
2319 union_type->data.unionation.resolve_loop_flag = true;2289 union_type->data.unionation.resolve_loop_flag = true;
23202290
2321 AstNode *decl_node = union_type->data.unionation.decl_node;
2322 assert(decl_node->type == NodeTypeContainerDecl);
2323
2324 assert(union_type->data.unionation.fields == nullptr);2291 assert(union_type->data.unionation.fields == nullptr);
2325 uint32_t field_count = (uint32_t)decl_node->data.container_decl.fields.length;2292 uint32_t field_count = (uint32_t)decl_node->data.container_decl.fields.length;
2326 if (field_count == 0) {2293 if (field_count == 0) {
...@@ -2380,14 +2347,13 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -2380,14 +2347,13 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
2380 tag_type->size_in_bits = tag_int_type->size_in_bits;2347 tag_type->size_in_bits = tag_int_type->size_in_bits;
23812348
2382 tag_type->data.enumeration.tag_int_type = tag_int_type;2349 tag_type->data.enumeration.tag_int_type = tag_int_type;
2383 tag_type->data.enumeration.zero_bits_known = true;2350 tag_type->data.enumeration.resolve_status = ResolveStatusSizeKnown;
2384 tag_type->data.enumeration.decl_node = decl_node;2351 tag_type->data.enumeration.decl_node = decl_node;
2385 tag_type->data.enumeration.layout = ContainerLayoutAuto;2352 tag_type->data.enumeration.layout = ContainerLayoutAuto;
2386 tag_type->data.enumeration.src_field_count = field_count;2353 tag_type->data.enumeration.src_field_count = field_count;
2387 tag_type->data.enumeration.fields = allocate<TypeEnumField>(field_count);2354 tag_type->data.enumeration.fields = allocate<TypeEnumField>(field_count);
2388 tag_type->data.enumeration.fields_by_name.init(field_count);2355 tag_type->data.enumeration.fields_by_name.init(field_count);
2389 tag_type->data.enumeration.decls_scope = union_type->data.unionation.decls_scope;2356 tag_type->data.enumeration.decls_scope = union_type->data.unionation.decls_scope;
2390 tag_type->data.enumeration.complete = true;
2391 } else if (enum_type_node != nullptr) {2357 } else if (enum_type_node != nullptr) {
2392 ZigType *enum_type = analyze_type_expr(g, scope, enum_type_node);2358 ZigType *enum_type = analyze_type_expr(g, scope, enum_type_node);
2393 if (type_is_invalid(enum_type)) {2359 if (type_is_invalid(enum_type)) {
...@@ -3185,9 +3151,8 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {...@@ -3185,9 +3151,8 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
3185 ZigType *explicit_type = nullptr;3151 ZigType *explicit_type = nullptr;
3186 if (var_decl->type) {3152 if (var_decl->type) {
3187 if (tld_var->analyzing_type) {3153 if (tld_var->analyzing_type) {
3188 ErrorMsg *msg = add_node_error(g, var_decl->type,3154 g->trace_err = add_node_error(g, var_decl->type,
3189 buf_sprintf("type of '%s' depends on itself", buf_ptr(tld_var->base.name)));3155 buf_sprintf("type of '%s' depends on itself", buf_ptr(tld_var->base.name)));
3190 emit_error_notes_for_ref_stack(g, msg);
3191 explicit_type = g->builtin_types.entry_invalid;3156 explicit_type = g->builtin_types.entry_invalid;
3192 } else {3157 } else {
3193 tld_var->analyzing_type = true;3158 tld_var->analyzing_type = true;
...@@ -3386,7 +3351,6 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node) {...@@ -3386,7 +3351,6 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node) {
33863351
3387 assert(tld->resolution != TldResolutionResolving);3352 assert(tld->resolution != TldResolutionResolving);
3388 tld->resolution = TldResolutionResolving;3353 tld->resolution = TldResolutionResolving;
3389 g->tld_ref_source_node_stack.append(source_node);
33903354
3391 switch (tld->id) {3355 switch (tld->id) {
3392 case TldIdVar:3356 case TldIdVar:
...@@ -3424,7 +3388,10 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node) {...@@ -3424,7 +3388,10 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node) {
3424 }3388 }
34253389
3426 tld->resolution = TldResolutionOk;3390 tld->resolution = TldResolutionOk;
3427 g->tld_ref_source_node_stack.pop();3391
3392 if (g->trace_err != nullptr && source_node != nullptr) {
3393 g->trace_err = add_error_note(g, g->trace_err, source_node, buf_create_from_str("referenced here"));
3394 }
3428}3395}
34293396
3430Tld *find_container_decl(CodeGen *g, ScopeDecls *decls_scope, Buf *name) {3397Tld *find_container_decl(CodeGen *g, ScopeDecls *decls_scope, Buf *name) {
...@@ -3550,7 +3517,7 @@ TypeUnionField *find_union_field_by_tag(ZigType *type_entry, const BigInt *tag)...@@ -3550,7 +3517,7 @@ TypeUnionField *find_union_field_by_tag(ZigType *type_entry, const BigInt *tag)
3550}3517}
35513518
3552TypeEnumField *find_enum_field_by_tag(ZigType *enum_type, const BigInt *tag) {3519TypeEnumField *find_enum_field_by_tag(ZigType *enum_type, const BigInt *tag) {
3553 assert(enum_type->data.enumeration.zero_bits_known);3520 assert(type_is_resolved(enum_type, ResolveStatusZeroBitsKnown));
3554 for (uint32_t i = 0; i < enum_type->data.enumeration.src_field_count; i += 1) {3521 for (uint32_t i = 0; i < enum_type->data.enumeration.src_field_count; i += 1) {
3555 TypeEnumField *field = &enum_type->data.enumeration.fields[i];3522 TypeEnumField *field = &enum_type->data.enumeration.fields[i];
3556 if (bigint_cmp(&field->value, tag) == CmpEQ) {3523 if (bigint_cmp(&field->value, tag) == CmpEQ) {
...@@ -3619,43 +3586,6 @@ ZigType *container_ref_type(ZigType *type_entry) {...@@ -3619,43 +3586,6 @@ ZigType *container_ref_type(ZigType *type_entry) {
3619 type_entry->data.pointer.child_type : type_entry;3586 type_entry->data.pointer.child_type : type_entry;
3620}3587}
36213588
3622Error resolve_container_type(CodeGen *g, ZigType *type_entry) {
3623 switch (type_entry->id) {
3624 case ZigTypeIdStruct:
3625 return resolve_struct_type(g, type_entry);
3626 case ZigTypeIdEnum:
3627 return resolve_enum_zero_bits(g, type_entry);
3628 case ZigTypeIdUnion:
3629 return resolve_union_type(g, type_entry);
3630 case ZigTypeIdPointer:
3631 case ZigTypeIdMetaType:
3632 case ZigTypeIdVoid:
3633 case ZigTypeIdBool:
3634 case ZigTypeIdUnreachable:
3635 case ZigTypeIdInt:
3636 case ZigTypeIdFloat:
3637 case ZigTypeIdArray:
3638 case ZigTypeIdComptimeFloat:
3639 case ZigTypeIdComptimeInt:
3640 case ZigTypeIdEnumLiteral:
3641 case ZigTypeIdUndefined:
3642 case ZigTypeIdNull:
3643 case ZigTypeIdOptional:
3644 case ZigTypeIdErrorUnion:
3645 case ZigTypeIdErrorSet:
3646 case ZigTypeIdFn:
3647 case ZigTypeIdBoundFn:
3648 case ZigTypeIdInvalid:
3649 case ZigTypeIdArgTuple:
3650 case ZigTypeIdOpaque:
3651 case ZigTypeIdVector:
3652 case ZigTypeIdFnFrame:
3653 case ZigTypeIdAnyFrame:
3654 zig_unreachable();
3655 }
3656 zig_unreachable();
3657}
3658
3659ZigType *get_src_ptr_type(ZigType *type) {3589ZigType *get_src_ptr_type(ZigType *type) {
3660 if (type->id == ZigTypeIdPointer) return type;3590 if (type->id == ZigTypeIdPointer) return type;
3661 if (type->id == ZigTypeIdFn) return type;3591 if (type->id == ZigTypeIdFn) return type;
...@@ -3906,7 +3836,7 @@ static void analyze_fn_ir(CodeGen *g, ZigFn *fn, AstNode *return_type_node) {...@@ -3906,7 +3836,7 @@ static void analyze_fn_ir(CodeGen *g, ZigFn *fn, AstNode *return_type_node) {
3906 &fn->analyzed_executable, fn_type_id->return_type, return_type_node);3836 &fn->analyzed_executable, fn_type_id->return_type, return_type_node);
3907 fn->src_implicit_return_type = block_return_type;3837 fn->src_implicit_return_type = block_return_type;
39083838
3909 if (type_is_invalid(block_return_type) || fn->analyzed_executable.invalid) {3839 if (type_is_invalid(block_return_type) || fn->analyzed_executable.first_err_trace_msg != nullptr) {
3910 assert(g->errors.length > 0);3840 assert(g->errors.length > 0);
3911 fn->anal_state = FnAnalStateInvalid;3841 fn->anal_state = FnAnalStateInvalid;
3912 return;3842 return;
...@@ -3990,7 +3920,7 @@ static void analyze_fn_body(CodeGen *g, ZigFn *fn_table_entry) {...@@ -3990,7 +3920,7 @@ static void analyze_fn_body(CodeGen *g, ZigFn *fn_table_entry) {
3990 assert(!fn_type->data.fn.is_generic);3920 assert(!fn_type->data.fn.is_generic);
39913921
3992 ir_gen_fn(g, fn_table_entry);3922 ir_gen_fn(g, fn_table_entry);
3993 if (fn_table_entry->ir_executable.invalid) {3923 if (fn_table_entry->ir_executable.first_err_trace_msg != nullptr) {
3994 fn_table_entry->anal_state = FnAnalStateInvalid;3924 fn_table_entry->anal_state = FnAnalStateInvalid;
3995 return;3925 return;
3996 }3926 }
...@@ -4128,12 +4058,14 @@ void semantic_analyze(CodeGen *g) {...@@ -4128,12 +4058,14 @@ void semantic_analyze(CodeGen *g) {
4128 {4058 {
4129 for (; g->resolve_queue_index < g->resolve_queue.length; g->resolve_queue_index += 1) {4059 for (; g->resolve_queue_index < g->resolve_queue.length; g->resolve_queue_index += 1) {
4130 Tld *tld = g->resolve_queue.at(g->resolve_queue_index);4060 Tld *tld = g->resolve_queue.at(g->resolve_queue_index);
4061 g->trace_err = nullptr;
4131 AstNode *source_node = nullptr;4062 AstNode *source_node = nullptr;
4132 resolve_top_level_decl(g, tld, source_node);4063 resolve_top_level_decl(g, tld, source_node);
4133 }4064 }
41344065
4135 for (; g->fn_defs_index < g->fn_defs.length; g->fn_defs_index += 1) {4066 for (; g->fn_defs_index < g->fn_defs.length; g->fn_defs_index += 1) {
4136 ZigFn *fn_entry = g->fn_defs.at(g->fn_defs_index);4067 ZigFn *fn_entry = g->fn_defs.at(g->fn_defs_index);
4068 g->trace_err = nullptr;
4137 analyze_fn_body(g, fn_entry);4069 analyze_fn_body(g, fn_entry);
4138 }4070 }
4139 }4071 }
...@@ -4145,6 +4077,7 @@ void semantic_analyze(CodeGen *g) {...@@ -4145,6 +4077,7 @@ void semantic_analyze(CodeGen *g) {
4145 // second pass over functions for detecting async4077 // second pass over functions for detecting async
4146 for (g->fn_defs_index = 0; g->fn_defs_index < g->fn_defs.length; g->fn_defs_index += 1) {4078 for (g->fn_defs_index = 0; g->fn_defs_index < g->fn_defs.length; g->fn_defs_index += 1) {
4147 ZigFn *fn = g->fn_defs.at(g->fn_defs_index);4079 ZigFn *fn = g->fn_defs.at(g->fn_defs_index);
4080 g->trace_err = nullptr;
4148 analyze_fn_async(g, fn, true);4081 analyze_fn_async(g, fn, true);
4149 if (fn_is_async(fn) && fn->non_async_node != nullptr) {4082 if (fn_is_async(fn) && fn->non_async_node != nullptr) {
4150 ErrorMsg *msg = add_node_error(g, fn->proto_node,4083 ErrorMsg *msg = add_node_error(g, fn->proto_node,
...@@ -5143,34 +5076,6 @@ ConstExprValue *create_const_arg_tuple(CodeGen *g, size_t arg_index_start, size_...@@ -5143,34 +5076,6 @@ ConstExprValue *create_const_arg_tuple(CodeGen *g, size_t arg_index_start, size_
5143}5076}
51445077
51455078
5146void init_const_undefined(CodeGen *g, ConstExprValue *const_val) {
5147 Error err;
5148 ZigType *wanted_type = const_val->type;
5149 if (wanted_type->id == ZigTypeIdArray) {
5150 const_val->special = ConstValSpecialStatic;
5151 const_val->data.x_array.special = ConstArraySpecialUndef;
5152 } else if (wanted_type->id == ZigTypeIdStruct) {
5153 if ((err = ensure_complete_type(g, wanted_type))) {
5154 return;
5155 }
5156
5157 const_val->special = ConstValSpecialStatic;
5158 size_t field_count = wanted_type->data.structure.src_field_count;
5159 const_val->data.x_struct.fields = create_const_vals(field_count);
5160 for (size_t i = 0; i < field_count; i += 1) {
5161 ConstExprValue *field_val = &const_val->data.x_struct.fields[i];
5162 field_val->type = wanted_type->data.structure.fields[i].type_entry;
5163 assert(field_val->type);
5164 init_const_undefined(g, field_val);
5165 field_val->parent.id = ConstParentIdStruct;
5166 field_val->parent.data.p_struct.struct_val = const_val;
5167 field_val->parent.data.p_struct.field_index = i;
5168 }
5169 } else {
5170 const_val->special = ConstValSpecialUndef;
5171 }
5172}
5173
5174ConstExprValue *create_const_vals(size_t count) {5079ConstExprValue *create_const_vals(size_t count) {
5175 ConstGlobalRefs *global_refs = allocate<ConstGlobalRefs>(count);5080 ConstGlobalRefs *global_refs = allocate<ConstGlobalRefs>(count);
5176 ConstExprValue *vals = allocate<ConstExprValue>(count);5081 ConstExprValue *vals = allocate<ConstExprValue>(count);
...@@ -5180,10 +5085,6 @@ ConstExprValue *create_const_vals(size_t count) {...@@ -5180,10 +5085,6 @@ ConstExprValue *create_const_vals(size_t count) {
5180 return vals;5085 return vals;
5181}5086}
51825087
5183Error ensure_complete_type(CodeGen *g, ZigType *type_entry) {
5184 return type_resolve(g, type_entry, ResolveStatusSizeKnown);
5185}
5186
5187static ZigType *get_async_fn_type(CodeGen *g, ZigType *orig_fn_type) {5088static ZigType *get_async_fn_type(CodeGen *g, ZigType *orig_fn_type) {
5188 if (orig_fn_type->data.fn.fn_type_id.cc == CallingConventionAsync)5089 if (orig_fn_type->data.fn.fn_type_id.cc == CallingConventionAsync)
5189 return orig_fn_type;5090 return orig_fn_type;
...@@ -5197,27 +5098,6 @@ static ZigType *get_async_fn_type(CodeGen *g, ZigType *orig_fn_type) {...@@ -5197,27 +5098,6 @@ static ZigType *get_async_fn_type(CodeGen *g, ZigType *orig_fn_type) {
5197 return fn_type;5098 return fn_type;
5198}5099}
51995100
5200static void emit_error_notes_for_type_loop(CodeGen *g, ErrorMsg *msg, ZigType *stop_type,
5201 ZigType *ty, AstNode *src_node)
5202{
5203 ErrorMsg *note = add_error_note(g, msg, src_node,
5204 buf_sprintf("when analyzing type '%s' here", buf_ptr(&ty->name)));
5205 if (ty == stop_type)
5206 return;
5207 switch (ty->id) {
5208 case ZigTypeIdFnFrame: {
5209 ty->data.frame.reported_loop_err = true;
5210 ZigType *depending_type = ty->data.frame.resolve_loop_type;
5211 if (depending_type == nullptr)
5212 return;
5213 emit_error_notes_for_type_loop(g, note, stop_type,
5214 depending_type, ty->data.frame.resolve_loop_src_node);
5215 }
5216 default:
5217 return;
5218 }
5219}
5220
5221static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {5101static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {
5222 Error err;5102 Error err;
52235103
...@@ -5230,13 +5110,8 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {...@@ -5230,13 +5110,8 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {
5230 if (frame_type->data.frame.resolve_loop_type != nullptr) {5110 if (frame_type->data.frame.resolve_loop_type != nullptr) {
5231 if (!frame_type->data.frame.reported_loop_err) {5111 if (!frame_type->data.frame.reported_loop_err) {
5232 frame_type->data.frame.reported_loop_err = true;5112 frame_type->data.frame.reported_loop_err = true;
5233 ErrorMsg *msg = add_node_error(g, fn->proto_node,5113 g->trace_err = add_node_error(g, fn->proto_node,
5234 buf_sprintf("'%s' depends on itself", buf_ptr(&frame_type->name)));5114 buf_sprintf("'%s' depends on itself", buf_ptr(&frame_type->name)));
5235 emit_error_notes_for_type_loop(g, msg,
5236 frame_type,
5237 frame_type->data.frame.resolve_loop_type,
5238 frame_type->data.frame.resolve_loop_src_node);
5239 emit_error_notes_for_ref_stack(g, msg);
5240 }5115 }
5241 return ErrorSemanticAnalyzeFail;5116 return ErrorSemanticAnalyzeFail;
5242 }5117 }
...@@ -5252,11 +5127,9 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {...@@ -5252,11 +5127,9 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {
5252 return ErrorSemanticAnalyzeFail;5127 return ErrorSemanticAnalyzeFail;
5253 break;5128 break;
5254 case FnAnalStateProbing: {5129 case FnAnalStateProbing: {
5255 ErrorMsg *msg = add_node_error(g, fn->proto_node,5130 g->trace_err = add_node_error(g, fn->proto_node,
5256 buf_sprintf("cannot resolve '%s': function not fully analyzed yet",5131 buf_sprintf("cannot resolve '%s': function not fully analyzed yet",
5257 buf_ptr(&frame_type->name)));5132 buf_ptr(&frame_type->name)));
5258 ir_add_analysis_trace(fn->ir_executable.analysis, msg,
5259 buf_sprintf("depends on its own frame here"));
5260 return ErrorSemanticAnalyzeFail;5133 return ErrorSemanticAnalyzeFail;
5261 }5134 }
5262 }5135 }
...@@ -5327,10 +5200,8 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {...@@ -5327,10 +5200,8 @@ static Error resolve_async_frame(CodeGen *g, ZigType *frame_type) {
5327 if (callee->anal_state == FnAnalStateProbing) {5200 if (callee->anal_state == FnAnalStateProbing) {
5328 ErrorMsg *msg = add_node_error(g, fn->proto_node,5201 ErrorMsg *msg = add_node_error(g, fn->proto_node,
5329 buf_sprintf("unable to determine async function frame of '%s'", buf_ptr(&fn->symbol_name)));5202 buf_sprintf("unable to determine async function frame of '%s'", buf_ptr(&fn->symbol_name)));
5330 ErrorMsg *note = add_error_note(g, msg, call->base.source_node,5203 g->trace_err = add_error_note(g, msg, call->base.source_node,
5331 buf_sprintf("analysis of function '%s' depends on the frame", buf_ptr(&callee->symbol_name)));5204 buf_sprintf("analysis of function '%s' depends on the frame", buf_ptr(&callee->symbol_name)));
5332 ir_add_analysis_trace(callee->ir_executable.analysis, note,
5333 buf_sprintf("depends on the frame here"));
5334 return ErrorSemanticAnalyzeFail;5205 return ErrorSemanticAnalyzeFail;
5335 }5206 }
53365207
...@@ -6229,6 +6100,40 @@ bool zig_llvm_fn_key_eql(ZigLLVMFnKey a, ZigLLVMFnKey b) {...@@ -6229,6 +6100,40 @@ bool zig_llvm_fn_key_eql(ZigLLVMFnKey a, ZigLLVMFnKey b) {
6229 zig_unreachable();6100 zig_unreachable();
6230}6101}
62316102
6103static void init_const_undefined(CodeGen *g, ConstExprValue *const_val) {
6104 Error err;
6105 ZigType *wanted_type = const_val->type;
6106 if (wanted_type->id == ZigTypeIdArray) {
6107 const_val->special = ConstValSpecialStatic;
6108 const_val->data.x_array.special = ConstArraySpecialUndef;
6109 } else if (wanted_type->id == ZigTypeIdStruct) {
6110 if ((err = type_resolve(g, wanted_type, ResolveStatusZeroBitsKnown))) {
6111 return;
6112 }
6113
6114 const_val->special = ConstValSpecialStatic;
6115 size_t field_count = wanted_type->data.structure.src_field_count;
6116 const_val->data.x_struct.fields = create_const_vals(field_count);
6117 for (size_t i = 0; i < field_count; i += 1) {
6118 ConstExprValue *field_val = &const_val->data.x_struct.fields[i];
6119 field_val->type = wanted_type->data.structure.fields[i].type_entry;
6120 assert(field_val->type);
6121 init_const_undefined(g, field_val);
6122 field_val->parent.id = ConstParentIdStruct;
6123 field_val->parent.data.p_struct.struct_val = const_val;
6124 field_val->parent.data.p_struct.field_index = i;
6125 }
6126 } else {
6127 const_val->special = ConstValSpecialUndef;
6128 }
6129}
6130
6131void expand_undef_struct(CodeGen *g, ConstExprValue *const_val) {
6132 if (const_val->special == ConstValSpecialUndef) {
6133 init_const_undefined(g, const_val);
6134 }
6135}
6136
6232// Canonicalize the array value as ConstArraySpecialNone6137// Canonicalize the array value as ConstArraySpecialNone
6233void expand_undef_array(CodeGen *g, ConstExprValue *const_val) {6138void expand_undef_array(CodeGen *g, ConstExprValue *const_val) {
6234 size_t elem_count;6139 size_t elem_count;
...@@ -6707,19 +6612,6 @@ bool ptr_allows_addr_zero(ZigType *ptr_type) {...@@ -6707,19 +6612,6 @@ bool ptr_allows_addr_zero(ZigType *ptr_type) {
6707 return false;6612 return false;
6708}6613}
67096614
6710void emit_error_notes_for_ref_stack(CodeGen *g, ErrorMsg *msg) {
6711 size_t i = g->tld_ref_source_node_stack.length;
6712 for (;;) {
6713 if (i == 0)
6714 break;
6715 i -= 1;
6716 AstNode *source_node = g->tld_ref_source_node_stack.at(i);
6717 if (source_node) {
6718 msg = add_error_note(g, msg, source_node, buf_sprintf("referenced here"));
6719 }
6720 }
6721}
6722
6723Buf *type_bare_name(ZigType *type_entry) {6615Buf *type_bare_name(ZigType *type_entry) {
6724 if (is_slice(type_entry)) {6616 if (is_slice(type_entry)) {
6725 return &type_entry->name;6617 return &type_entry->name;
...@@ -7122,10 +7014,9 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type, ResolveS...@@ -7122,10 +7014,9 @@ static void resolve_llvm_types_struct(CodeGen *g, ZigType *struct_type, ResolveS
7122 struct_type->data.structure.resolve_status = ResolveStatusLLVMFull;7014 struct_type->data.structure.resolve_status = ResolveStatusLLVMFull;
7123}7015}
71247016
7125static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type) {7017static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type, ResolveStatus wanted_resolve_status) {
7126 assert(!enum_type->data.enumeration.is_invalid);7018 assert(enum_type->data.enumeration.resolve_status >= ResolveStatusSizeKnown);
7127 assert(enum_type->data.enumeration.complete);7019 if (enum_type->data.enumeration.resolve_status >= wanted_resolve_status) return;
7128 if (enum_type->llvm_di_type != nullptr) return;
71297020
7130 Scope *scope = &enum_type->data.enumeration.decls_scope->base;7021 Scope *scope = &enum_type->data.enumeration.decls_scope->base;
7131 ZigType *import = get_scope_import(scope);7022 ZigType *import = get_scope_import(scope);
...@@ -7146,6 +7037,7 @@ static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type) {...@@ -7146,6 +7037,7 @@ static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type) {
7146 debug_align_in_bits,7037 debug_align_in_bits,
7147 ZigLLVM_DIFlags_Zero,7038 ZigLLVM_DIFlags_Zero,
7148 nullptr, di_element_types, (int)debug_field_count, 0, nullptr, "");7039 nullptr, di_element_types, (int)debug_field_count, 0, nullptr, "");
7040 enum_type->data.enumeration.resolve_status = ResolveStatusLLVMFull;
7149 return;7041 return;
7150 }7042 }
71517043
...@@ -7178,6 +7070,7 @@ static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type) {...@@ -7178,6 +7070,7 @@ static void resolve_llvm_types_enum(CodeGen *g, ZigType *enum_type) {
7178 get_llvm_di_type(g, tag_int_type), "");7070 get_llvm_di_type(g, tag_int_type), "");
71797071
7180 enum_type->llvm_di_type = tag_di_type;7072 enum_type->llvm_di_type = tag_di_type;
7073 enum_type->data.enumeration.resolve_status = ResolveStatusLLVMFull;
7181}7074}
71827075
7183static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveStatus wanted_resolve_status) {7076static void resolve_llvm_types_union(CodeGen *g, ZigType *union_type, ResolveStatus wanted_resolve_status) {
...@@ -7909,7 +7802,7 @@ static void resolve_llvm_types(CodeGen *g, ZigType *type, ResolveStatus wanted_r...@@ -7909,7 +7802,7 @@ static void resolve_llvm_types(CodeGen *g, ZigType *type, ResolveStatus wanted_r
7909 else7802 else
7910 return resolve_llvm_types_struct(g, type, wanted_resolve_status, nullptr);7803 return resolve_llvm_types_struct(g, type, wanted_resolve_status, nullptr);
7911 case ZigTypeIdEnum:7804 case ZigTypeIdEnum:
7912 return resolve_llvm_types_enum(g, type);7805 return resolve_llvm_types_enum(g, type, wanted_resolve_status);
7913 case ZigTypeIdUnion:7806 case ZigTypeIdUnion:
7914 return resolve_llvm_types_union(g, type, wanted_resolve_status);7807 return resolve_llvm_types_union(g, type, wanted_resolve_status);
7915 case ZigTypeIdPointer:7808 case ZigTypeIdPointer:
src/analyze.hpp+1-5
...@@ -14,7 +14,6 @@ void semantic_analyze(CodeGen *g);...@@ -14,7 +14,6 @@ void semantic_analyze(CodeGen *g);
14ErrorMsg *add_node_error(CodeGen *g, const AstNode *node, Buf *msg);14ErrorMsg *add_node_error(CodeGen *g, const AstNode *node, Buf *msg);
15ErrorMsg *add_token_error(CodeGen *g, ZigType *owner, Token *token, Buf *msg);15ErrorMsg *add_token_error(CodeGen *g, ZigType *owner, Token *token, Buf *msg);
16ErrorMsg *add_error_note(CodeGen *g, ErrorMsg *parent_msg, const AstNode *node, Buf *msg);16ErrorMsg *add_error_note(CodeGen *g, ErrorMsg *parent_msg, const AstNode *node, Buf *msg);
17void emit_error_notes_for_ref_stack(CodeGen *g, ErrorMsg *msg);
18ZigType *new_type_table_entry(ZigTypeId id);17ZigType *new_type_table_entry(ZigTypeId id);
19ZigType *get_fn_frame_type(CodeGen *g, ZigFn *fn);18ZigType *get_fn_frame_type(CodeGen *g, ZigFn *fn);
20ZigType *get_pointer_to_type(CodeGen *g, ZigType *child_type, bool is_const);19ZigType *get_pointer_to_type(CodeGen *g, ZigType *child_type, bool is_const);
...@@ -71,7 +70,6 @@ bool type_is_complete(ZigType *type_entry);...@@ -71,7 +70,6 @@ bool type_is_complete(ZigType *type_entry);
71bool type_is_resolved(ZigType *type_entry, ResolveStatus status);70bool type_is_resolved(ZigType *type_entry, ResolveStatus status);
72bool type_is_invalid(ZigType *type_entry);71bool type_is_invalid(ZigType *type_entry);
73bool type_is_global_error_set(ZigType *err_set_type);72bool type_is_global_error_set(ZigType *err_set_type);
74Error resolve_container_type(CodeGen *g, ZigType *type_entry);
75ScopeDecls *get_container_scope(ZigType *type_entry);73ScopeDecls *get_container_scope(ZigType *type_entry);
76TypeStructField *find_struct_type_field(ZigType *type_entry, Buf *name);74TypeStructField *find_struct_type_field(ZigType *type_entry, Buf *name);
77TypeEnumField *find_enum_type_field(ZigType *enum_type, Buf *name);75TypeEnumField *find_enum_type_field(ZigType *enum_type, Buf *name);
...@@ -95,7 +93,6 @@ ZigFn *create_fn(CodeGen *g, AstNode *proto_node);...@@ -95,7 +93,6 @@ ZigFn *create_fn(CodeGen *g, AstNode *proto_node);
95ZigFn *create_fn_raw(CodeGen *g, FnInline inline_value);93ZigFn *create_fn_raw(CodeGen *g, FnInline inline_value);
96void init_fn_type_id(FnTypeId *fn_type_id, AstNode *proto_node, size_t param_count_alloc);94void init_fn_type_id(FnTypeId *fn_type_id, AstNode *proto_node, size_t param_count_alloc);
97AstNode *get_param_decl_node(ZigFn *fn_entry, size_t index);95AstNode *get_param_decl_node(ZigFn *fn_entry, size_t index);
98Error ATTRIBUTE_MUST_USE ensure_complete_type(CodeGen *g, ZigType *type_entry);
99Error ATTRIBUTE_MUST_USE type_resolve(CodeGen *g, ZigType *type_entry, ResolveStatus status);96Error ATTRIBUTE_MUST_USE type_resolve(CodeGen *g, ZigType *type_entry, ResolveStatus status);
100void complete_enum(CodeGen *g, ZigType *enum_type);97void complete_enum(CodeGen *g, ZigType *enum_type);
101bool ir_get_var_is_comptime(ZigVar *var);98bool ir_get_var_is_comptime(ZigVar *var);
...@@ -169,12 +166,11 @@ ConstExprValue *create_const_slice(CodeGen *g, ConstExprValue *array_val, size_t...@@ -169,12 +166,11 @@ ConstExprValue *create_const_slice(CodeGen *g, ConstExprValue *array_val, size_t
169void init_const_arg_tuple(CodeGen *g, ConstExprValue *const_val, size_t arg_index_start, size_t arg_index_end);166void init_const_arg_tuple(CodeGen *g, ConstExprValue *const_val, size_t arg_index_start, size_t arg_index_end);
170ConstExprValue *create_const_arg_tuple(CodeGen *g, size_t arg_index_start, size_t arg_index_end);167ConstExprValue *create_const_arg_tuple(CodeGen *g, size_t arg_index_start, size_t arg_index_end);
171168
172void init_const_undefined(CodeGen *g, ConstExprValue *const_val);
173
174ConstExprValue *create_const_vals(size_t count);169ConstExprValue *create_const_vals(size_t count);
175170
176ZigType *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits);171ZigType *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits);
177void expand_undef_array(CodeGen *g, ConstExprValue *const_val);172void expand_undef_array(CodeGen *g, ConstExprValue *const_val);
173void expand_undef_struct(CodeGen *g, ConstExprValue *const_val);
178void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value);174void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value);
179175
180const char *type_id_name(ZigTypeId id);176const char *type_id_name(ZigTypeId id);
src/ir.cpp+72-61
...@@ -234,6 +234,7 @@ static ConstExprValue *const_ptr_pointee_unchecked(CodeGen *g, ConstExprValue *c...@@ -234,6 +234,7 @@ static ConstExprValue *const_ptr_pointee_unchecked(CodeGen *g, ConstExprValue *c
234 }234 }
235 case ConstPtrSpecialBaseStruct: {235 case ConstPtrSpecialBaseStruct: {
236 ConstExprValue *struct_val = const_val->data.x_ptr.data.base_struct.struct_val;236 ConstExprValue *struct_val = const_val->data.x_ptr.data.base_struct.struct_val;
237 expand_undef_struct(g, struct_val);
237 result = &struct_val->data.x_struct.fields[const_val->data.x_ptr.data.base_struct.field_index];238 result = &struct_val->data.x_struct.fields[const_val->data.x_ptr.data.base_struct.field_index];
238 break;239 break;
239 }240 }
...@@ -8111,7 +8112,9 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, Scope *sc...@@ -8111,7 +8112,9 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, Scope *sc
8111 ir_build_reset_result(irb, scope, node, result_loc);8112 ir_build_reset_result(irb, scope, node, result_loc);
8112 }8113 }
8113 IrInstruction *result = ir_gen_node_raw(irb, node, scope, lval, result_loc);8114 IrInstruction *result = ir_gen_node_raw(irb, node, scope, lval, result_loc);
8114 irb->exec->invalid = irb->exec->invalid || (result == irb->codegen->invalid_instruction);8115 if (result == irb->codegen->invalid_instruction) {
8116 src_assert(irb->exec->first_err_trace_msg != nullptr, node);
8117 }
8115 return result;8118 return result;
8116}8119}
81178120
...@@ -8119,21 +8122,20 @@ static IrInstruction *ir_gen_node(IrBuilder *irb, AstNode *node, Scope *scope) {...@@ -8119,21 +8122,20 @@ static IrInstruction *ir_gen_node(IrBuilder *irb, AstNode *node, Scope *scope) {
8119 return ir_gen_node_extra(irb, node, scope, LValNone, nullptr);8122 return ir_gen_node_extra(irb, node, scope, LValNone, nullptr);
8120}8123}
81218124
8122static void invalidate_exec(IrExecutable *exec) {8125static void invalidate_exec(IrExecutable *exec, ErrorMsg *msg) {
8123 if (exec->invalid)8126 if (exec->first_err_trace_msg != nullptr)
8124 return;8127 return;
81258128
8126 exec->invalid = true;8129 exec->first_err_trace_msg = msg;
81278130
8128 for (size_t i = 0; i < exec->tld_list.length; i += 1) {8131 for (size_t i = 0; i < exec->tld_list.length; i += 1) {
8129 exec->tld_list.items[i]->resolution = TldResolutionInvalid;8132 exec->tld_list.items[i]->resolution = TldResolutionInvalid;
8130 }8133 }
81318134
8132 if (exec->source_exec != nullptr)8135 if (exec->source_exec != nullptr)
8133 invalidate_exec(exec->source_exec);8136 invalidate_exec(exec->source_exec, msg);
8134}8137}
81358138
8136
8137bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_executable) {8139bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_executable) {
8138 assert(node->owner);8140 assert(node->owner);
81398141
...@@ -8151,8 +8153,10 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec...@@ -8151,8 +8153,10 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
81518153
8152 IrInstruction *result = ir_gen_node_extra(irb, node, scope, LValNone, nullptr);8154 IrInstruction *result = ir_gen_node_extra(irb, node, scope, LValNone, nullptr);
8153 assert(result);8155 assert(result);
8154 if (irb->exec->invalid)8156 if (irb->exec->first_err_trace_msg != nullptr) {
8157 codegen->trace_err = irb->exec->first_err_trace_msg;
8155 return false;8158 return false;
8159 }
81568160
8157 if (!instr_is_unreachable(result)) {8161 if (!instr_is_unreachable(result)) {
8158 ir_mark_gen(ir_build_add_implicit_return_type(irb, scope, result->source_node, result));8162 ir_mark_gen(ir_build_add_implicit_return_type(irb, scope, result->source_node, result));
...@@ -8174,25 +8178,9 @@ bool ir_gen_fn(CodeGen *codegen, ZigFn *fn_entry) {...@@ -8174,25 +8178,9 @@ bool ir_gen_fn(CodeGen *codegen, ZigFn *fn_entry) {
8174 return ir_gen(codegen, body_node, fn_entry->child_scope, ir_executable);8178 return ir_gen(codegen, body_node, fn_entry->child_scope, ir_executable);
8175}8179}
81768180
8177static void ir_add_call_stack_errors(CodeGen *codegen, IrExecutable *exec, ErrorMsg *err_msg, int limit) {
8178 if (!exec || !exec->source_node || limit < 0) return;
8179 add_error_note(codegen, err_msg, exec->source_node, buf_sprintf("called from here"));
8180
8181 ir_add_call_stack_errors(codegen, exec->parent_exec, err_msg, limit - 1);
8182}
8183
8184void ir_add_analysis_trace(IrAnalyze *ira, ErrorMsg *err_msg, Buf *text) {
8185 IrInstruction *old_instruction = ira->old_irb.current_basic_block->instruction_list.at(ira->instruction_index);
8186 add_error_note(ira->codegen, err_msg, old_instruction->source_node, text);
8187 ir_add_call_stack_errors(ira->codegen, ira->new_irb.exec, err_msg, 10);
8188}
8189
8190static ErrorMsg *exec_add_error_node(CodeGen *codegen, IrExecutable *exec, AstNode *source_node, Buf *msg) {8181static ErrorMsg *exec_add_error_node(CodeGen *codegen, IrExecutable *exec, AstNode *source_node, Buf *msg) {
8191 invalidate_exec(exec);
8192 ErrorMsg *err_msg = add_node_error(codegen, source_node, msg);8182 ErrorMsg *err_msg = add_node_error(codegen, source_node, msg);
8193 if (exec->parent_exec) {8183 invalidate_exec(exec, err_msg);
8194 ir_add_call_stack_errors(codegen, exec, err_msg, 10);
8195 }
8196 return err_msg;8184 return err_msg;
8197}8185}
81988186
...@@ -10634,7 +10622,7 @@ static void ir_finish_bb(IrAnalyze *ira) {...@@ -10634,7 +10622,7 @@ static void ir_finish_bb(IrAnalyze *ira) {
1063410622
10635static IrInstruction *ir_unreach_error(IrAnalyze *ira) {10623static IrInstruction *ir_unreach_error(IrAnalyze *ira) {
10636 ira->old_bb_index = SIZE_MAX;10624 ira->old_bb_index = SIZE_MAX;
10637 ira->new_irb.exec->invalid = true;10625 assert(ira->new_irb.exec->first_err_trace_msg != nullptr);
10638 return ira->codegen->unreach_instruction;10626 return ira->codegen->unreach_instruction;
10639}10627}
1064010628
...@@ -10761,8 +10749,11 @@ ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *nod...@@ -10761,8 +10749,11 @@ ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *nod
10761 ir_executable->begin_scope = scope;10749 ir_executable->begin_scope = scope;
10762 ir_gen(codegen, node, scope, ir_executable);10750 ir_gen(codegen, node, scope, ir_executable);
1076310751
10764 if (ir_executable->invalid)10752 if (ir_executable->first_err_trace_msg != nullptr) {
10753 codegen->trace_err = add_error_note(codegen, ir_executable->first_err_trace_msg,
10754 source_node, buf_create_from_str("called from here"));
10765 return &codegen->invalid_instruction->value;10755 return &codegen->invalid_instruction->value;
10756 }
1076610757
10767 if (codegen->verbose_ir) {10758 if (codegen->verbose_ir) {
10768 fprintf(stderr, "\nSource: ");10759 fprintf(stderr, "\nSource: ");
...@@ -10783,8 +10774,9 @@ ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *nod...@@ -10783,8 +10774,9 @@ ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *nod
10783 analyzed_executable->backward_branch_quota = backward_branch_quota;10774 analyzed_executable->backward_branch_quota = backward_branch_quota;
10784 analyzed_executable->begin_scope = scope;10775 analyzed_executable->begin_scope = scope;
10785 ZigType *result_type = ir_analyze(codegen, ir_executable, analyzed_executable, expected_type, expected_type_source_node);10776 ZigType *result_type = ir_analyze(codegen, ir_executable, analyzed_executable, expected_type, expected_type_source_node);
10786 if (type_is_invalid(result_type))10777 if (type_is_invalid(result_type)) {
10787 return &codegen->invalid_instruction->value;10778 return &codegen->invalid_instruction->value;
10779 }
1078810780
10789 if (codegen->verbose_ir) {10781 if (codegen->verbose_ir) {
10790 fprintf(stderr, "{ // (analyzed)\n");10782 fprintf(stderr, "{ // (analyzed)\n");
...@@ -11322,7 +11314,7 @@ static IrInstruction *ir_analyze_undefined_to_anything(IrAnalyze *ira, IrInstruc...@@ -11322,7 +11314,7 @@ static IrInstruction *ir_analyze_undefined_to_anything(IrAnalyze *ira, IrInstruc
11322 IrInstruction *target, ZigType *wanted_type)11314 IrInstruction *target, ZigType *wanted_type)
11323{11315{
11324 IrInstruction *result = ir_const(ira, source_instr, wanted_type);11316 IrInstruction *result = ir_const(ira, source_instr, wanted_type);
11325 init_const_undefined(ira->codegen, &result->value);11317 result->value.special = ConstValSpecialUndef;
11326 return result;11318 return result;
11327}11319}
1132811320
...@@ -11461,7 +11453,7 @@ static IrInstruction *ir_analyze_int_to_enum(IrAnalyze *ira, IrInstruction *sour...@@ -11461,7 +11453,7 @@ static IrInstruction *ir_analyze_int_to_enum(IrAnalyze *ira, IrInstruction *sour
1146111453
11462 ZigType *actual_type = target->value.type;11454 ZigType *actual_type = target->value.type;
1146311455
11464 if ((err = ensure_complete_type(ira->codegen, wanted_type)))11456 if ((err = type_resolve(ira->codegen, wanted_type, ResolveStatusSizeKnown)))
11465 return ira->codegen->invalid_instruction;11457 return ira->codegen->invalid_instruction;
1146611458
11467 if (actual_type != wanted_type->data.enumeration.tag_int_type) {11459 if (actual_type != wanted_type->data.enumeration.tag_int_type) {
...@@ -12719,7 +12711,9 @@ static IrInstruction *ir_analyze_instruction_return(IrAnalyze *ira, IrInstructio...@@ -12719,7 +12711,9 @@ static IrInstruction *ir_analyze_instruction_return(IrAnalyze *ira, IrInstructio
12719 if (type_is_invalid(operand->value.type))12711 if (type_is_invalid(operand->value.type))
12720 return ir_unreach_error(ira);12712 return ir_unreach_error(ira);
1272112713
12722 if (!instr_is_comptime(operand) && handle_is_ptr(ira->explicit_return_type)) {12714 if (!instr_is_comptime(operand) && ira->explicit_return_type != nullptr &&
12715 handle_is_ptr(ira->explicit_return_type))
12716 {
12723 // result location mechanism took care of it.12717 // result location mechanism took care of it.
12724 IrInstruction *result = ir_build_return(&ira->new_irb, instruction->base.scope,12718 IrInstruction *result = ir_build_return(&ira->new_irb, instruction->base.scope,
12725 instruction->base.source_node, nullptr);12719 instruction->base.source_node, nullptr);
...@@ -15513,8 +15507,9 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c...@@ -15513,8 +15507,9 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
15513 ira->codegen->memoized_fn_eval_table.put(exec_scope, result);15507 ira->codegen->memoized_fn_eval_table.put(exec_scope, result);
15514 }15508 }
1551515509
15516 if (type_is_invalid(result->type))15510 if (type_is_invalid(result->type)) {
15517 return ira->codegen->invalid_instruction;15511 return ira->codegen->invalid_instruction;
15512 }
15518 }15513 }
1551915514
15520 IrInstruction *new_instruction = ir_const(ira, &call_instruction->base, result->type);15515 IrInstruction *new_instruction = ir_const(ira, &call_instruction->base, result->type);
...@@ -16727,7 +16722,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct...@@ -16727,7 +16722,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
16727 return ira->codegen->invalid_instruction;16722 return ira->codegen->invalid_instruction;
1672816723
16729 bool safety_check_on = elem_ptr_instruction->safety_check_on;16724 bool safety_check_on = elem_ptr_instruction->safety_check_on;
16730 if ((err = ensure_complete_type(ira->codegen, return_type->data.pointer.child_type)))16725 if ((err = type_resolve(ira->codegen, return_type->data.pointer.child_type, ResolveStatusSizeKnown)))
16731 return ira->codegen->invalid_instruction;16726 return ira->codegen->invalid_instruction;
1673216727
16733 uint64_t elem_size = type_size(ira->codegen, return_type->data.pointer.child_type);16728 uint64_t elem_size = type_size(ira->codegen, return_type->data.pointer.child_type);
...@@ -17113,7 +17108,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_...@@ -17113,7 +17108,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
17113 Error err;17108 Error err;
1711417109
17115 ZigType *bare_type = container_ref_type(container_type);17110 ZigType *bare_type = container_ref_type(container_type);
17116 if ((err = ensure_complete_type(ira->codegen, bare_type)))17111 if ((err = type_resolve(ira->codegen, bare_type, ResolveStatusSizeKnown)))
17117 return ira->codegen->invalid_instruction;17112 return ira->codegen->invalid_instruction;
1711817113
17119 assert(container_ptr->value.type->id == ZigTypeIdPointer);17114 assert(container_ptr->value.type->id == ZigTypeIdPointer);
...@@ -17243,15 +17238,15 @@ static void add_link_lib_symbol(IrAnalyze *ira, Buf *lib_name, Buf *symbol_name,...@@ -17243,15 +17238,15 @@ static void add_link_lib_symbol(IrAnalyze *ira, Buf *lib_name, Buf *symbol_name,
17243}17238}
1724417239
17245static IrInstruction *ir_error_dependency_loop(IrAnalyze *ira, IrInstruction *source_instr) {17240static IrInstruction *ir_error_dependency_loop(IrAnalyze *ira, IrInstruction *source_instr) {
17246 ErrorMsg *msg = ir_add_error(ira, source_instr, buf_sprintf("dependency loop detected"));17241 ir_add_error(ira, source_instr, buf_sprintf("dependency loop detected"));
17247 emit_error_notes_for_ref_stack(ira->codegen, msg);
17248 return ira->codegen->invalid_instruction;17242 return ira->codegen->invalid_instruction;
17249}17243}
1725017244
17251static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_instruction, Tld *tld) {17245static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_instruction, Tld *tld) {
17252 resolve_top_level_decl(ira->codegen, tld, source_instruction->source_node);17246 resolve_top_level_decl(ira->codegen, tld, source_instruction->source_node);
17253 if (tld->resolution == TldResolutionInvalid)17247 if (tld->resolution == TldResolutionInvalid) {
17254 return ira->codegen->invalid_instruction;17248 return ira->codegen->invalid_instruction;
17249 }
1725517250
17256 switch (tld->id) {17251 switch (tld->id) {
17257 case TldIdContainer:17252 case TldIdContainer:
...@@ -17396,7 +17391,7 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc...@@ -17396,7 +17391,7 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
17396 return ira->codegen->invalid_instruction;17391 return ira->codegen->invalid_instruction;
17397 } else if (is_container(child_type)) {17392 } else if (is_container(child_type)) {
17398 if (child_type->id == ZigTypeIdEnum) {17393 if (child_type->id == ZigTypeIdEnum) {
17399 if ((err = ensure_complete_type(ira->codegen, child_type)))17394 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusSizeKnown)))
17400 return ira->codegen->invalid_instruction;17395 return ira->codegen->invalid_instruction;
1740117396
17402 TypeEnumField *field = find_enum_type_field(child_type, field_name);17397 TypeEnumField *field = find_enum_type_field(child_type, field_name);
...@@ -17425,7 +17420,7 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc...@@ -17425,7 +17420,7 @@ static IrInstruction *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstruc
17425 (child_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr ||17420 (child_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr ||
17426 child_type->data.unionation.decl_node->data.container_decl.auto_enum))17421 child_type->data.unionation.decl_node->data.container_decl.auto_enum))
17427 {17422 {
17428 if ((err = ensure_complete_type(ira->codegen, child_type)))17423 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusSizeKnown)))
17429 return ira->codegen->invalid_instruction;17424 return ira->codegen->invalid_instruction;
17430 TypeUnionField *field = find_union_type_field(child_type, field_name);17425 TypeUnionField *field = find_union_type_field(child_type, field_name);
17431 if (field) {17426 if (field) {
...@@ -18008,7 +18003,7 @@ static IrInstruction *ir_analyze_instruction_array_type(IrAnalyze *ira,...@@ -18008,7 +18003,7 @@ static IrInstruction *ir_analyze_instruction_array_type(IrAnalyze *ira,
18008 case ZigTypeIdFnFrame:18003 case ZigTypeIdFnFrame:
18009 case ZigTypeIdAnyFrame:18004 case ZigTypeIdAnyFrame:
18010 {18005 {
18011 if ((err = ensure_complete_type(ira->codegen, child_type)))18006 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusSizeKnown)))
18012 return ira->codegen->invalid_instruction;18007 return ira->codegen->invalid_instruction;
18013 ZigType *result_type = get_array_type(ira->codegen, child_type, size);18008 ZigType *result_type = get_array_type(ira->codegen, child_type, size);
18014 return ir_const_type(ira, &array_type_instruction->base, result_type);18009 return ir_const_type(ira, &array_type_instruction->base, result_type);
...@@ -18024,7 +18019,7 @@ static IrInstruction *ir_analyze_instruction_size_of(IrAnalyze *ira,...@@ -18024,7 +18019,7 @@ static IrInstruction *ir_analyze_instruction_size_of(IrAnalyze *ira,
18024 IrInstruction *type_value = size_of_instruction->type_value->child;18019 IrInstruction *type_value = size_of_instruction->type_value->child;
18025 ZigType *type_entry = ir_resolve_type(ira, type_value);18020 ZigType *type_entry = ir_resolve_type(ira, type_value);
1802618021
18027 if ((err = ensure_complete_type(ira->codegen, type_entry)))18022 if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusSizeKnown)))
18028 return ira->codegen->invalid_instruction;18023 return ira->codegen->invalid_instruction;
1802918024
18030 switch (type_entry->id) {18025 switch (type_entry->id) {
...@@ -18529,7 +18524,7 @@ static IrInstruction *ir_analyze_instruction_switch_target(IrAnalyze *ira,...@@ -18529,7 +18524,7 @@ static IrInstruction *ir_analyze_instruction_switch_target(IrAnalyze *ira,
18529 if (pointee_val->special == ConstValSpecialRuntime)18524 if (pointee_val->special == ConstValSpecialRuntime)
18530 pointee_val = nullptr;18525 pointee_val = nullptr;
18531 }18526 }
18532 if ((err = ensure_complete_type(ira->codegen, target_type)))18527 if ((err = type_resolve(ira->codegen, target_type, ResolveStatusSizeKnown)))
18533 return ira->codegen->invalid_instruction;18528 return ira->codegen->invalid_instruction;
1853418529
18535 switch (target_type->id) {18530 switch (target_type->id) {
...@@ -19276,8 +19271,7 @@ static IrInstruction *ir_analyze_instruction_compile_err(IrAnalyze *ira,...@@ -19276,8 +19271,7 @@ static IrInstruction *ir_analyze_instruction_compile_err(IrAnalyze *ira,
19276 if (!msg_buf)19271 if (!msg_buf)
19277 return ira->codegen->invalid_instruction;19272 return ira->codegen->invalid_instruction;
1927819273
19279 ErrorMsg *msg = ir_add_error(ira, &instruction->base, msg_buf);19274 ir_add_error(ira, &instruction->base, msg_buf);
19280 emit_error_notes_for_ref_stack(ira->codegen, msg);
1928119275
19282 return ira->codegen->invalid_instruction;19276 return ira->codegen->invalid_instruction;
19283}19277}
...@@ -19391,7 +19385,7 @@ static IrInstruction *ir_analyze_instruction_field_parent_ptr(IrAnalyze *ira,...@@ -19391,7 +19385,7 @@ static IrInstruction *ir_analyze_instruction_field_parent_ptr(IrAnalyze *ira,
19391 return ira->codegen->invalid_instruction;19385 return ira->codegen->invalid_instruction;
19392 }19386 }
1939319387
19394 if ((err = ensure_complete_type(ira->codegen, container_type)))19388 if ((err = type_resolve(ira->codegen, container_type, ResolveStatusSizeKnown)))
19395 return ira->codegen->invalid_instruction;19389 return ira->codegen->invalid_instruction;
1939619390
19397 TypeStructField *field = find_struct_type_field(container_type, field_name);19391 TypeStructField *field = find_struct_type_field(container_type, field_name);
...@@ -19470,7 +19464,7 @@ static TypeStructField *validate_byte_offset(IrAnalyze *ira,...@@ -19470,7 +19464,7 @@ static TypeStructField *validate_byte_offset(IrAnalyze *ira,
19470 return nullptr;19464 return nullptr;
1947119465
19472 Error err;19466 Error err;
19473 if ((err = ensure_complete_type(ira->codegen, container_type)))19467 if ((err = type_resolve(ira->codegen, container_type, ResolveStatusSizeKnown)))
19474 return nullptr;19468 return nullptr;
1947519469
19476 Buf *field_name = ir_resolve_str(ira, field_name_value);19470 Buf *field_name = ir_resolve_str(ira, field_name_value);
...@@ -19574,7 +19568,7 @@ static ZigType *ir_type_info_get_type(IrAnalyze *ira, const char *type_name, Zig...@@ -19574,7 +19568,7 @@ static ZigType *ir_type_info_get_type(IrAnalyze *ira, const char *type_name, Zig
1957419568
19575 ZigVar *var = tld->var;19569 ZigVar *var = tld->var;
1957619570
19577 if ((err = ensure_complete_type(ira->codegen, var->const_value->type)))19571 if ((err = type_resolve(ira->codegen, var->const_value->type, ResolveStatusSizeKnown)))
19578 return ira->codegen->builtin_types.entry_invalid;19572 return ira->codegen->builtin_types.entry_invalid;
1957919573
19580 assert(var->const_value->type->id == ZigTypeIdMetaType);19574 assert(var->const_value->type->id == ZigTypeIdMetaType);
...@@ -19594,15 +19588,15 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr...@@ -19594,15 +19588,15 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr
19594 ensure_field_index(type_info_declaration_type, "data", 2);19588 ensure_field_index(type_info_declaration_type, "data", 2);
1959519589
19596 ZigType *type_info_declaration_data_type = ir_type_info_get_type(ira, "Data", type_info_declaration_type);19590 ZigType *type_info_declaration_data_type = ir_type_info_get_type(ira, "Data", type_info_declaration_type);
19597 if ((err = ensure_complete_type(ira->codegen, type_info_declaration_data_type)))19591 if ((err = type_resolve(ira->codegen, type_info_declaration_data_type, ResolveStatusSizeKnown)))
19598 return err;19592 return err;
1959919593
19600 ZigType *type_info_fn_decl_type = ir_type_info_get_type(ira, "FnDecl", type_info_declaration_data_type);19594 ZigType *type_info_fn_decl_type = ir_type_info_get_type(ira, "FnDecl", type_info_declaration_data_type);
19601 if ((err = ensure_complete_type(ira->codegen, type_info_fn_decl_type)))19595 if ((err = type_resolve(ira->codegen, type_info_fn_decl_type, ResolveStatusSizeKnown)))
19602 return err;19596 return err;
1960319597
19604 ZigType *type_info_fn_decl_inline_type = ir_type_info_get_type(ira, "Inline", type_info_fn_decl_type);19598 ZigType *type_info_fn_decl_inline_type = ir_type_info_get_type(ira, "Inline", type_info_fn_decl_type);
19605 if ((err = ensure_complete_type(ira->codegen, type_info_fn_decl_inline_type)))19599 if ((err = type_resolve(ira->codegen, type_info_fn_decl_inline_type, ResolveStatusSizeKnown)))
19606 return err;19600 return err;
1960719601
19608 // Loop through our declarations once to figure out how many declarations we will generate info for.19602 // Loop through our declarations once to figure out how many declarations we will generate info for.
...@@ -19673,7 +19667,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr...@@ -19673,7 +19667,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr
19673 case TldIdVar:19667 case TldIdVar:
19674 {19668 {
19675 ZigVar *var = ((TldVar *)curr_entry->value)->var;19669 ZigVar *var = ((TldVar *)curr_entry->value)->var;
19676 if ((err = ensure_complete_type(ira->codegen, var->const_value->type)))19670 if ((err = type_resolve(ira->codegen, var->const_value->type, ResolveStatusSizeKnown)))
19677 return ErrorSemanticAnalyzeFail;19671 return ErrorSemanticAnalyzeFail;
1967819672
19679 if (var->const_value->type->id == ZigTypeIdMetaType) {19673 if (var->const_value->type->id == ZigTypeIdMetaType) {
...@@ -19799,7 +19793,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr...@@ -19799,7 +19793,7 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInstruction *source_instr
19799 case TldIdContainer:19793 case TldIdContainer:
19800 {19794 {
19801 ZigType *type_entry = ((TldContainer *)curr_entry->value)->type_entry;19795 ZigType *type_entry = ((TldContainer *)curr_entry->value)->type_entry;
19802 if ((err = ensure_complete_type(ira->codegen, type_entry)))19796 if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusSizeKnown)))
19803 return ErrorSemanticAnalyzeFail;19797 return ErrorSemanticAnalyzeFail;
1980419798
19805 // This is a type.19799 // This is a type.
...@@ -19855,7 +19849,7 @@ static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_ty...@@ -19855,7 +19849,7 @@ static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_ty
19855 return nullptr;19849 return nullptr;
1985619850
19857 ZigType *type_info_pointer_type = ir_type_info_get_type(ira, "Pointer", nullptr);19851 ZigType *type_info_pointer_type = ir_type_info_get_type(ira, "Pointer", nullptr);
19858 assertNoError(ensure_complete_type(ira->codegen, type_info_pointer_type));19852 assertNoError(type_resolve(ira->codegen, type_info_pointer_type, ResolveStatusSizeKnown));
1985919853
19860 ConstExprValue *result = create_const_vals(1);19854 ConstExprValue *result = create_const_vals(1);
19861 result->special = ConstValSpecialStatic;19855 result->special = ConstValSpecialStatic;
...@@ -19867,7 +19861,7 @@ static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_ty...@@ -19867,7 +19861,7 @@ static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, ZigType *ptr_ty
19867 // size: Size19861 // size: Size
19868 ensure_field_index(result->type, "size", 0);19862 ensure_field_index(result->type, "size", 0);
19869 ZigType *type_info_pointer_size_type = ir_type_info_get_type(ira, "Size", type_info_pointer_type);19863 ZigType *type_info_pointer_size_type = ir_type_info_get_type(ira, "Size", type_info_pointer_type);
19870 assertNoError(ensure_complete_type(ira->codegen, type_info_pointer_size_type));19864 assertNoError(type_resolve(ira->codegen, type_info_pointer_size_type, ResolveStatusSizeKnown));
19871 fields[0].special = ConstValSpecialStatic;19865 fields[0].special = ConstValSpecialStatic;
19872 fields[0].type = type_info_pointer_size_type;19866 fields[0].type = type_info_pointer_size_type;
19873 bigint_init_unsigned(&fields[0].data.x_enum_tag, size_enum_index);19867 bigint_init_unsigned(&fields[0].data.x_enum_tag, size_enum_index);
...@@ -22021,7 +22015,7 @@ static IrInstruction *ir_analyze_instruction_member_count(IrAnalyze *ira, IrInst...@@ -22021,7 +22015,7 @@ static IrInstruction *ir_analyze_instruction_member_count(IrAnalyze *ira, IrInst
22021 return ira->codegen->invalid_instruction;22015 return ira->codegen->invalid_instruction;
22022 ZigType *container_type = ir_resolve_type(ira, container);22016 ZigType *container_type = ir_resolve_type(ira, container);
2202322017
22024 if ((err = ensure_complete_type(ira->codegen, container_type)))22018 if ((err = type_resolve(ira->codegen, container_type, ResolveStatusSizeKnown)))
22025 return ira->codegen->invalid_instruction;22019 return ira->codegen->invalid_instruction;
2202622020
22027 uint64_t result;22021 uint64_t result;
...@@ -22057,7 +22051,7 @@ static IrInstruction *ir_analyze_instruction_member_type(IrAnalyze *ira, IrInstr...@@ -22057,7 +22051,7 @@ static IrInstruction *ir_analyze_instruction_member_type(IrAnalyze *ira, IrInstr
22057 if (type_is_invalid(container_type))22051 if (type_is_invalid(container_type))
22058 return ira->codegen->invalid_instruction;22052 return ira->codegen->invalid_instruction;
2205922053
22060 if ((err = ensure_complete_type(ira->codegen, container_type)))22054 if ((err = type_resolve(ira->codegen, container_type, ResolveStatusSizeKnown)))
22061 return ira->codegen->invalid_instruction;22055 return ira->codegen->invalid_instruction;
2206222056
2206322057
...@@ -22100,7 +22094,7 @@ static IrInstruction *ir_analyze_instruction_member_name(IrAnalyze *ira, IrInstr...@@ -22100,7 +22094,7 @@ static IrInstruction *ir_analyze_instruction_member_name(IrAnalyze *ira, IrInstr
22100 if (type_is_invalid(container_type))22094 if (type_is_invalid(container_type))
22101 return ira->codegen->invalid_instruction;22095 return ira->codegen->invalid_instruction;
2210222096
22103 if ((err = ensure_complete_type(ira->codegen, container_type)))22097 if ((err = type_resolve(ira->codegen, container_type, ResolveStatusSizeKnown)))
22104 return ira->codegen->invalid_instruction;22098 return ira->codegen->invalid_instruction;
2210522099
22106 uint64_t member_index;22100 uint64_t member_index;
...@@ -23309,8 +23303,10 @@ static void buf_write_value_bytes_array(CodeGen *codegen, uint8_t *buf, ConstExp...@@ -23309,8 +23303,10 @@ static void buf_write_value_bytes_array(CodeGen *codegen, uint8_t *buf, ConstExp
23309}23303}
2331023304
23311static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue *val) {23305static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue *val) {
23312 if (val->special == ConstValSpecialUndef)23306 if (val->special == ConstValSpecialUndef) {
23307 expand_undef_struct(codegen, val);
23313 val->special = ConstValSpecialStatic;23308 val->special = ConstValSpecialStatic;
23309 }
23314 assert(val->special == ConstValSpecialStatic);23310 assert(val->special == ConstValSpecialStatic);
23315 switch (val->type->id) {23311 switch (val->type->id) {
23316 case ZigTypeIdInvalid:23312 case ZigTypeIdInvalid:
...@@ -23746,8 +23742,9 @@ static IrInstruction *ir_analyze_instruction_decl_ref(IrAnalyze *ira,...@@ -23746,8 +23742,9 @@ static IrInstruction *ir_analyze_instruction_decl_ref(IrAnalyze *ira,
23746 IrInstructionDeclRef *instruction)23742 IrInstructionDeclRef *instruction)
23747{23743{
23748 IrInstruction *ref_instruction = ir_analyze_decl_ref(ira, &instruction->base, instruction->tld);23744 IrInstruction *ref_instruction = ir_analyze_decl_ref(ira, &instruction->base, instruction->tld);
23749 if (type_is_invalid(ref_instruction->value.type))23745 if (type_is_invalid(ref_instruction->value.type)) {
23750 return ira->codegen->invalid_instruction;23746 return ira->codegen->invalid_instruction;
23747 }
2375123748
23752 if (instruction->lval == LValPtr) {23749 if (instruction->lval == LValPtr) {
23753 return ref_instruction;23750 return ref_instruction;
...@@ -23954,7 +23951,7 @@ static IrInstruction *ir_analyze_instruction_tag_type(IrAnalyze *ira, IrInstruct...@@ -23954,7 +23951,7 @@ static IrInstruction *ir_analyze_instruction_tag_type(IrAnalyze *ira, IrInstruct
23954 return ira->codegen->invalid_instruction;23951 return ira->codegen->invalid_instruction;
2395523952
23956 if (enum_type->id == ZigTypeIdEnum) {23953 if (enum_type->id == ZigTypeIdEnum) {
23957 if ((err = ensure_complete_type(ira->codegen, enum_type)))23954 if ((err = type_resolve(ira->codegen, enum_type, ResolveStatusSizeKnown)))
23958 return ira->codegen->invalid_instruction;23955 return ira->codegen->invalid_instruction;
2395923956
23960 return ir_const_type(ira, &instruction->base, enum_type->data.enumeration.tag_int_type);23957 return ir_const_type(ira, &instruction->base, enum_type->data.enumeration.tag_int_type);
...@@ -25100,7 +25097,7 @@ static IrInstruction *ir_analyze_instruction_base(IrAnalyze *ira, IrInstruction...@@ -25100,7 +25097,7 @@ static IrInstruction *ir_analyze_instruction_base(IrAnalyze *ira, IrInstruction
25100ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_exec,25097ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_exec,
25101 ZigType *expected_type, AstNode *expected_type_source_node)25098 ZigType *expected_type, AstNode *expected_type_source_node)
25102{25099{
25103 assert(!old_exec->invalid);25100 assert(old_exec->first_err_trace_msg == nullptr);
25104 assert(expected_type == nullptr || !type_is_invalid(expected_type));25101 assert(expected_type == nullptr || !type_is_invalid(expected_type));
2510525102
25106 IrAnalyze *ira = allocate<IrAnalyze>(1);25103 IrAnalyze *ira = allocate<IrAnalyze>(1);
...@@ -25147,6 +25144,15 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_...@@ -25147,6 +25144,15 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_
25147 old_instruction->child = new_instruction;25144 old_instruction->child = new_instruction;
2514825145
25149 if (type_is_invalid(new_instruction->value.type)) {25146 if (type_is_invalid(new_instruction->value.type)) {
25147 if (new_exec->first_err_trace_msg != nullptr) {
25148 ira->codegen->trace_err = new_exec->first_err_trace_msg;
25149 } else {
25150 new_exec->first_err_trace_msg = ira->codegen->trace_err;
25151 }
25152 if (new_exec->first_err_trace_msg != nullptr) {
25153 new_exec->first_err_trace_msg = add_error_note(ira->codegen, new_exec->first_err_trace_msg,
25154 old_instruction->source_node, buf_create_from_str("referenced here"));
25155 }
25150 return ira->codegen->builtin_types.entry_invalid;25156 return ira->codegen->builtin_types.entry_invalid;
25151 }25157 }
2515225158
...@@ -25158,7 +25164,12 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_...@@ -25158,7 +25164,12 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_
25158 ira->instruction_index += 1;25164 ira->instruction_index += 1;
25159 }25165 }
2516025166
25161 if (new_exec->invalid) {25167 if (new_exec->first_err_trace_msg != nullptr) {
25168 codegen->trace_err = new_exec->first_err_trace_msg;
25169 if (codegen->trace_err != nullptr) {
25170 codegen->trace_err = add_error_note(codegen, codegen->trace_err,
25171 new_exec->source_node, buf_create_from_str("referenced here"));
25172 }
25162 return ira->codegen->builtin_types.entry_invalid;25173 return ira->codegen->builtin_types.entry_invalid;
25163 } else if (ira->src_implicit_return_type_list.length == 0) {25174 } else if (ira->src_implicit_return_type_list.length == 0) {
25164 return codegen->builtin_types.entry_unreachable;25175 return codegen->builtin_types.entry_unreachable;
src/ir.hpp-2
...@@ -28,6 +28,4 @@ ConstExprValue *const_ptr_pointee(IrAnalyze *ira, CodeGen *codegen, ConstExprVal...@@ -28,6 +28,4 @@ ConstExprValue *const_ptr_pointee(IrAnalyze *ira, CodeGen *codegen, ConstExprVal
28 AstNode *source_node);28 AstNode *source_node);
29const char *float_op_to_name(BuiltinFnId op, bool llvm_name);29const char *float_op_to_name(BuiltinFnId op, bool llvm_name);
3030
31void ir_add_analysis_trace(IrAnalyze *ira, ErrorMsg *err_msg, Buf *text);
32
33#endif31#endif