authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-01 17:46:31-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-02 18:31:18-04:00
logd3f2fe2cef7de1173a038365f91e8a574a08f21a
tree51c8833d1ebcc92460fb1ee2eb3b05060fc97f4d
parent3dc8448680cfea2b55a6064c655e400e31e5d3dd
signaturelock-open Commit is signed but in an unrecognized format.

remove the lazy value stuff

let's try to keep this branch to solving one problem at a time

6 files changed, 196 insertions(+), 565 deletions(-)

src/all_types.hpp+1-42
...@@ -256,7 +256,6 @@ enum ConstValSpecial {...@@ -256,7 +256,6 @@ enum ConstValSpecial {
256 ConstValSpecialRuntime,256 ConstValSpecialRuntime,
257 ConstValSpecialStatic,257 ConstValSpecialStatic,
258 ConstValSpecialUndef,258 ConstValSpecialUndef,
259 ConstValSpecialLazy,
260};259};
261260
262enum RuntimeHintErrorUnion {261enum RuntimeHintErrorUnion {
...@@ -292,43 +291,6 @@ struct ConstGlobalRefs {...@@ -292,43 +291,6 @@ struct ConstGlobalRefs {
292 LLVMValueRef llvm_global;291 LLVMValueRef llvm_global;
293};292};
294293
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
332struct ConstExprValue {294struct ConstExprValue {
333 ZigType *type;295 ZigType *type;
334 ConstValSpecial special;296 ConstValSpecial special;
...@@ -356,7 +318,6 @@ struct ConstExprValue {...@@ -356,7 +318,6 @@ struct ConstExprValue {
356 ConstPtrValue x_ptr;318 ConstPtrValue x_ptr;
357 ConstArgTuple x_arg_tuple;319 ConstArgTuple x_arg_tuple;
358 Buf *x_enum_literal;320 Buf *x_enum_literal;
359 LazyValue *x_lazy;
360321
361 // populated if special == ConstValSpecialRuntime322 // populated if special == ConstValSpecialRuntime
362 RuntimeHintErrorUnion rh_error_union;323 RuntimeHintErrorUnion rh_error_union;
...@@ -398,7 +359,6 @@ enum TldResolution {...@@ -398,7 +359,6 @@ enum TldResolution {
398 TldResolutionUnresolved,359 TldResolutionUnresolved,
399 TldResolutionResolving,360 TldResolutionResolving,
400 TldResolutionInvalid,361 TldResolutionInvalid,
401 TldResolutionOkLazy,
402 TldResolutionOk,362 TldResolutionOk,
403};363};
404364
...@@ -1104,8 +1064,7 @@ struct ZigTypeArray {...@@ -1104,8 +1064,7 @@ struct ZigTypeArray {
11041064
1105struct TypeStructField {1065struct TypeStructField {
1106 Buf *name;1066 Buf *name;
1107 ZigType *type_entry; // available after ResolveStatusSizeKnown1067 ZigType *type_entry;
1108 ConstExprValue *type_val; // available after ResolveStatusZeroBitsKnown
1109 size_t src_index;1068 size_t src_index;
1110 size_t gen_index;1069 size_t gen_index;
1111 size_t offset; // byte offset from beginning of struct1070 size_t offset; // byte offset from beginning of struct
src/analyze.cpp+60-211
...@@ -902,10 +902,10 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {...@@ -902,10 +902,10 @@ ZigType *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
902 }902 }
903 buf_appendf(&fn_type->name, " %s", buf_ptr(&fn_type_id->return_type->name));903 buf_appendf(&fn_type->name, " %s", buf_ptr(&fn_type_id->return_type->name));
904904
905 // The fn_type is a pointer; not to be confused with the raw function type.
905 fn_type->size_in_bits = g->builtin_types.entry_usize->size_in_bits;906 fn_type->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
906 fn_type->abi_size = g->builtin_types.entry_usize->abi_size;907 fn_type->abi_size = g->builtin_types.entry_usize->abi_size;
907 // see also type_val_resolve_abi_align908 fn_type->abi_align = g->builtin_types.entry_usize->abi_align;
908 fn_type->abi_align = (fn_type_id->alignment == 0) ? 1 : fn_type_id->alignment;
909909
910 g->fn_type_table.put(&fn_type->data.fn.fn_type_id, fn_type);910 g->fn_type_table.put(&fn_type->data.fn.fn_type_id, fn_type);
911911
...@@ -963,134 +963,15 @@ ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind...@@ -963,134 +963,15 @@ ZigType *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind
963 return entry;963 return entry;
964}964}
965965
966static ConstExprValue *analyze_const_value_allow_lazy(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry,966static ConstExprValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry,
967 Buf *type_name, bool allow_lazy)967 Buf *type_name)
968{968{
969 size_t backward_branch_count = 0;969 size_t backward_branch_count = 0;
970 return ir_eval_const_value(g, scope, node, type_entry,970 return ir_eval_const_value(g, scope, node, type_entry,
971 &backward_branch_count, default_backward_branch_quota,971 &backward_branch_count, default_backward_branch_quota,
972 nullptr, nullptr, node, type_name, nullptr, nullptr, allow_lazy);972 nullptr, nullptr, node, type_name, nullptr, nullptr);
973}973}
974974
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
1094ZigType *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) {975ZigType *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) {
1095 ConstExprValue *result = analyze_const_value(g, scope, node, g->builtin_types.entry_type, nullptr);976 ConstExprValue *result = analyze_const_value(g, scope, node, g->builtin_types.entry_type, nullptr);
1096 if (type_is_invalid(result->type))977 if (type_is_invalid(result->type))
...@@ -1656,9 +1537,17 @@ ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_na...@@ -1656,9 +1537,17 @@ ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_na
1656 size_t next_offset = 0;1537 size_t next_offset = 0;
1657 for (size_t i = 0; i < field_count; i += 1) {1538 for (size_t i = 0; i < field_count; i += 1) {
1658 TypeStructField *field = &struct_type->data.structure.fields[i];1539 TypeStructField *field = &struct_type->data.structure.fields[i];
1540 if (field->gen_index == SIZE_MAX)
1541 continue;
1659 field->offset = next_offset;1542 field->offset = next_offset;
1660 size_t next_abi_align = (i + 1 == field_count) ?1543 size_t next_src_field_index = i + 1;
1661 abi_align : struct_type->data.structure.fields[i + 1].type_entry->abi_align;1544 for (; next_src_field_index < field_count; next_src_field_index += 1) {
1545 if (struct_type->data.structure.fields[next_src_field_index].gen_index != SIZE_MAX) {
1546 break;
1547 }
1548 }
1549 size_t next_abi_align = (next_src_field_index == field_count) ?
1550 abi_align : struct_type->data.structure.fields[next_src_field_index].type_entry->abi_align;
1662 next_offset = next_field_offset(next_offset, abi_align, field->type_entry->abi_size, next_abi_align);1551 next_offset = next_field_offset(next_offset, abi_align, field->type_entry->abi_size, next_abi_align);
1663 }1552 }
16641553
...@@ -1716,43 +1605,6 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {...@@ -1716,43 +1605,6 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
1716 size_t size_in_bits = 0;1605 size_t size_in_bits = 0;
1717 size_t abi_align = struct_type->abi_align;1606 size_t abi_align = struct_type->abi_align;
17181607
1719 // Resolve types for fields
1720 for (size_t i = 0; i < field_count; i += 1) {
1721 AstNode *field_source_node = decl_node->data.container_decl.fields.at(i);
1722 TypeStructField *field = &struct_type->data.structure.fields[i];
1723
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;
1730
1731 if ((err = type_resolve(g, field_type, ResolveStatusSizeKnown))) {
1732 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
1733 return err;
1734 }
1735
1736 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid) {
1737 return ErrorSemanticAnalyzeFail;
1738 }
1739
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 offsets1608 // Calculate offsets
1757 for (size_t i = 0; i < field_count; i += 1) {1609 for (size_t i = 0; i < field_count; i += 1) {
1758 TypeStructField *field = &struct_type->data.structure.fields[i];1610 TypeStructField *field = &struct_type->data.structure.fields[i];
...@@ -2186,8 +2038,6 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {...@@ -2186,8 +2038,6 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
2186static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {2038static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
2187 assert(struct_type->id == ZigTypeIdStruct);2039 assert(struct_type->id == ZigTypeIdStruct);
21882040
2189 Error err;
2190
2191 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)2041 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
2192 return ErrorSemanticAnalyzeFail;2042 return ErrorSemanticAnalyzeFail;
2193 if (struct_type->data.structure.resolve_status >= ResolveStatusZeroBitsKnown)2043 if (struct_type->data.structure.resolve_status >= ResolveStatusZeroBitsKnown)
...@@ -2238,36 +2088,29 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {...@@ -2238,36 +2088,29 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
2238 return ErrorSemanticAnalyzeFail;2088 return ErrorSemanticAnalyzeFail;
2239 }2089 }
22402090
2241 ConstExprValue *field_type_val = analyze_const_value_allow_lazy(g, scope,2091 ZigType *field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type);
2242 field_node->data.struct_field.type, g->builtin_types.entry_type, nullptr, true);2092 type_struct_field->type_entry = field_type;
2243 if (type_is_invalid(field_type_val->type)) {2093 if (type_is_invalid(field_type)) {
2244 struct_type->data.structure.resolve_status = ResolveStatusInvalid;2094 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2245 return ErrorSemanticAnalyzeFail;2095 return ErrorSemanticAnalyzeFail;
2246 }2096 }
2247 assert(field_type_val->special != ConstValSpecialRuntime);
2248 type_struct_field->type_val = field_type_val;
2249 type_struct_field->src_index = i;
2250 type_struct_field->gen_index = SIZE_MAX;
2251
2252 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)2097 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
2253 return ErrorSemanticAnalyzeFail;2098 return ErrorSemanticAnalyzeFail;
22542099
2100 type_struct_field->src_index = i;
2101 type_struct_field->gen_index = SIZE_MAX;
2102
2255 if (field_node->data.struct_field.value != nullptr) {2103 if (field_node->data.struct_field.value != nullptr) {
2256 add_node_error(g, field_node->data.struct_field.value,2104 add_node_error(g, field_node->data.struct_field.value,
2257 buf_sprintf("enums, not structs, support field assignment"));2105 buf_sprintf("enums, not structs, support field assignment"));
2258 }2106 }
2259 bool field_is_opaque_type;2107 if (field_type->id == ZigTypeIdOpaque) {
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) {
2265 add_node_error(g, field_node->data.struct_field.type,2108 add_node_error(g, field_node->data.struct_field.type,
2266 buf_sprintf("opaque types have unknown size and therefore cannot be directly embedded in structs"));2109 buf_sprintf("opaque types have unknown size and therefore cannot be directly embedded in structs"));
2267 struct_type->data.structure.resolve_status = ResolveStatusInvalid;2110 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2268 return ErrorSemanticAnalyzeFail;2111 return ErrorSemanticAnalyzeFail;
2269 }2112 }
2270 switch (type_val_resolve_requires_comptime(g, field_type_val)) {2113 switch (type_requires_comptime(g, field_type)) {
2271 case ReqCompTimeYes:2114 case ReqCompTimeYes:
2272 struct_type->data.structure.requires_comptime = true;2115 struct_type->data.structure.requires_comptime = true;
2273 break;2116 break;
...@@ -2278,12 +2121,7 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {...@@ -2278,12 +2121,7 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
2278 break;2121 break;
2279 }2122 }
22802123
2281 bool field_is_zero_bits;2124 if (!type_has_bits(field_type))
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)
2287 continue;2125 continue;
22882126
2289 type_struct_field->gen_index = gen_field_index;2127 type_struct_field->gen_index = gen_field_index;
...@@ -2338,7 +2176,31 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {...@@ -2338,7 +2176,31 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
2338 bool packed = struct_type->data.structure.layout == ContainerLayoutPacked;2176 bool packed = struct_type->data.structure.layout == ContainerLayoutPacked;
23392177
2340 for (size_t i = 0; i < field_count; i += 1) {2178 for (size_t i = 0; i < field_count; i += 1) {
2179 AstNode *field_source_node = decl_node->data.container_decl.fields.at(i);
2341 TypeStructField *field = &struct_type->data.structure.fields[i];2180 TypeStructField *field = &struct_type->data.structure.fields[i];
2181 ZigType *field_type = field->type_entry;
2182 assert(field_type != nullptr);
2183
2184 if ((err = type_resolve(g, field_type, ResolveStatusAlignmentKnown))) {
2185 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2186 return ErrorSemanticAnalyzeFail;
2187 }
2188
2189 if (struct_type->data.structure.layout == ContainerLayoutExtern &&
2190 !type_allowed_in_extern(g, field_type))
2191 {
2192 add_node_error(g, field_source_node,
2193 buf_sprintf("extern structs cannot contain fields of type '%s'",
2194 buf_ptr(&field_type->name)));
2195 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2196 return ErrorSemanticAnalyzeFail;
2197 } else if (packed) {
2198 if ((err = emit_error_unless_type_allowed_in_packed_struct(g, field_type, field_source_node))) {
2199 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2200 return ErrorSemanticAnalyzeFail;
2201 }
2202 }
2203
2342 if (field->gen_index == SIZE_MAX)2204 if (field->gen_index == SIZE_MAX)
2343 continue;2205 continue;
23442206
...@@ -2349,13 +2211,8 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {...@@ -2349,13 +2211,8 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
2349 }2211 }
2350 } else {2212 } else {
2351 // TODO: https://github.com/ziglang/zig/issues/15122213 // TODO: https://github.com/ziglang/zig/issues/1512
2352 size_t field_align;2214 if (field_type->abi_align > abi_align) {
2353 if ((err = type_val_resolve_abi_align(g, field->type_val, &field_align))) {2215 abi_align = field_type->abi_align;
2354 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2355 return err;
2356 }
2357 if (field_align > abi_align) {
2358 abi_align = field_align;
2359 }2216 }
2360 }2217 }
2361 }2218 }
...@@ -2993,7 +2850,7 @@ void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source...@@ -2993,7 +2850,7 @@ void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source
29932850
2994void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value) {2851void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value) {
2995 Tld *tld = get_container_scope(g->compile_var_import)->decl_table.get(name);2852 Tld *tld = get_container_scope(g->compile_var_import)->decl_table.get(name);
2996 resolve_top_level_decl(g, tld, tld->source_node, false);2853 resolve_top_level_decl(g, tld, tld->source_node);
2997 assert(tld->id == TldIdVar);2854 assert(tld->id == TldIdVar);
2998 TldVar *tld_var = (TldVar *)tld;2855 TldVar *tld_var = (TldVar *)tld;
2999 tld_var->var->const_value = value;2856 tld_var->var->const_value = value;
...@@ -3232,7 +3089,7 @@ ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf...@@ -3232,7 +3089,7 @@ ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf
3232 return variable_entry;3089 return variable_entry;
3233}3090}
32343091
3235static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) {3092static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
3236 AstNode *source_node = tld_var->base.source_node;3093 AstNode *source_node = tld_var->base.source_node;
3237 AstNodeVariableDeclaration *var_decl = &source_node->data.variable_declaration;3094 AstNodeVariableDeclaration *var_decl = &source_node->data.variable_declaration;
32383095
...@@ -3273,8 +3130,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) {...@@ -3273,8 +3130,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) {
3273 if (explicit_type && explicit_type->id == ZigTypeIdInvalid) {3130 if (explicit_type && explicit_type->id == ZigTypeIdInvalid) {
3274 implicit_type = explicit_type;3131 implicit_type = explicit_type;
3275 } else if (var_decl->expr) {3132 } else if (var_decl->expr) {
3276 init_value = analyze_const_value_allow_lazy(g, tld_var->base.parent_scope, var_decl->expr,3133 init_value = analyze_const_value(g, tld_var->base.parent_scope, var_decl->expr, explicit_type, var_decl->symbol);
3277 explicit_type, var_decl->symbol, allow_lazy);
3278 assert(init_value);3134 assert(init_value);
3279 implicit_type = init_value->type;3135 implicit_type = init_value->type;
32803136
...@@ -3337,11 +3193,11 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) {...@@ -3337,11 +3193,11 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) {
3337 g->global_vars.append(tld_var);3193 g->global_vars.append(tld_var);
3338}3194}
33393195
3340void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node, bool allow_lazy) {3196void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node) {
3341 bool want_resolve_lazy = tld->resolution == TldResolutionOkLazy && !allow_lazy;3197 if (tld->resolution != TldResolutionUnresolved)
3342 if (tld->resolution != TldResolutionUnresolved && !want_resolve_lazy)
3343 return;3198 return;
33443199
3200 assert(tld->resolution != TldResolutionResolving);
3345 tld->resolution = TldResolutionResolving;3201 tld->resolution = TldResolutionResolving;
3346 g->tld_ref_source_node_stack.append(source_node);3202 g->tld_ref_source_node_stack.append(source_node);
33473203
...@@ -3349,11 +3205,7 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node, bool all...@@ -3349,11 +3205,7 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node, bool all
3349 case TldIdVar:3205 case TldIdVar:
3350 {3206 {
3351 TldVar *tld_var = (TldVar *)tld;3207 TldVar *tld_var = (TldVar *)tld;
3352 if (want_resolve_lazy) {3208 resolve_decl_var(g, tld_var);
3353 ir_resolve_lazy(g, source_node, tld_var->var->const_value);
3354 } else {
3355 resolve_decl_var(g, tld_var, allow_lazy);
3356 }
3357 break;3209 break;
3358 }3210 }
3359 case TldIdFn:3211 case TldIdFn:
...@@ -3376,7 +3228,7 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node, bool all...@@ -3376,7 +3228,7 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node, bool all
3376 }3228 }
3377 }3229 }
33783230
3379 tld->resolution = allow_lazy ? TldResolutionOkLazy : TldResolutionOk;3231 tld->resolution = TldResolutionOk;
3380 g->tld_ref_source_node_stack.pop();3232 g->tld_ref_source_node_stack.pop();
3381}3233}
33823234
...@@ -4045,7 +3897,7 @@ void semantic_analyze(CodeGen *g) {...@@ -4045,7 +3897,7 @@ void semantic_analyze(CodeGen *g) {
4045 for (; g->resolve_queue_index < g->resolve_queue.length; g->resolve_queue_index += 1) {3897 for (; g->resolve_queue_index < g->resolve_queue.length; g->resolve_queue_index += 1) {
4046 Tld *tld = g->resolve_queue.at(g->resolve_queue_index);3898 Tld *tld = g->resolve_queue.at(g->resolve_queue_index);
4047 AstNode *source_node = nullptr;3899 AstNode *source_node = nullptr;
4048 resolve_top_level_decl(g, tld, source_node, false);3900 resolve_top_level_decl(g, tld, source_node);
4049 }3901 }
40503902
4051 for (; g->fn_defs_index < g->fn_defs.length; g->fn_defs_index += 1) {3903 for (; g->fn_defs_index < g->fn_defs.length; g->fn_defs_index += 1) {
...@@ -5479,9 +5331,6 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {...@@ -5479,9 +5331,6 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
5479 case ConstValSpecialRuntime:5331 case ConstValSpecialRuntime:
5480 buf_appendf(buf, "(runtime value)");5332 buf_appendf(buf, "(runtime value)");
5481 return;5333 return;
5482 case ConstValSpecialLazy:
5483 buf_appendf(buf, "(lazy value)");
5484 return;
5485 case ConstValSpecialUndef:5334 case ConstValSpecialUndef:
5486 buf_appendf(buf, "undefined");5335 buf_appendf(buf, "undefined");
5487 return;5336 return;
...@@ -6098,7 +5947,7 @@ bool type_ptr_eql(const ZigType *a, const ZigType *b) {...@@ -6098,7 +5947,7 @@ bool type_ptr_eql(const ZigType *a, const ZigType *b) {
60985947
6099ConstExprValue *get_builtin_value(CodeGen *codegen, const char *name) {5948ConstExprValue *get_builtin_value(CodeGen *codegen, const char *name) {
6100 Tld *tld = get_container_scope(codegen->compile_var_import)->decl_table.get(buf_create_from_str(name));5949 Tld *tld = get_container_scope(codegen->compile_var_import)->decl_table.get(buf_create_from_str(name));
6101 resolve_top_level_decl(codegen, tld, nullptr, false);5950 resolve_top_level_decl(codegen, tld, nullptr);
6102 assert(tld->id == TldIdVar);5951 assert(tld->id == TldIdVar);
6103 TldVar *tld_var = (TldVar *)tld;5952 TldVar *tld_var = (TldVar *)tld;
6104 ConstExprValue *var_value = tld_var->var->const_value;5953 ConstExprValue *var_value = tld_var->var->const_value;
src/analyze.hpp+1-1
...@@ -61,7 +61,7 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *abs_full_path, Bu...@@ -61,7 +61,7 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *abs_full_path, Bu
61ZigVar *find_variable(CodeGen *g, Scope *orig_context, Buf *name, ScopeFnDef **crossed_fndef_scope);61ZigVar *find_variable(CodeGen *g, Scope *orig_context, Buf *name, ScopeFnDef **crossed_fndef_scope);
62Tld *find_decl(CodeGen *g, Scope *scope, Buf *name);62Tld *find_decl(CodeGen *g, Scope *scope, Buf *name);
63Tld *find_container_decl(CodeGen *g, ScopeDecls *decls_scope, Buf *name);63Tld *find_container_decl(CodeGen *g, ScopeDecls *decls_scope, Buf *name);
64void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node, bool allow_lazy);64void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node);
6565
66ZigType *get_src_ptr_type(ZigType *type);66ZigType *get_src_ptr_type(ZigType *type);
67ZigType *get_codegen_ptr_type(ZigType *type);67ZigType *get_codegen_ptr_type(ZigType *type);
src/codegen.cpp+1-5
...@@ -3358,8 +3358,6 @@ static bool value_is_all_undef_array(ConstExprValue *const_val, size_t len) {...@@ -3358,8 +3358,6 @@ static bool value_is_all_undef_array(ConstExprValue *const_val, size_t len) {
33583358
3359static bool value_is_all_undef(ConstExprValue *const_val) {3359static bool value_is_all_undef(ConstExprValue *const_val) {
3360 switch (const_val->special) {3360 switch (const_val->special) {
3361 case ConstValSpecialLazy:
3362 zig_unreachable();
3363 case ConstValSpecialRuntime:3361 case ConstValSpecialRuntime:
3364 return false;3362 return false;
3365 case ConstValSpecialUndef:3363 case ConstValSpecialUndef:
...@@ -5824,7 +5822,6 @@ static LLVMValueRef gen_const_ptr_union_recursive(CodeGen *g, ConstExprValue *un...@@ -5824,7 +5822,6 @@ static LLVMValueRef gen_const_ptr_union_recursive(CodeGen *g, ConstExprValue *un
58245822
5825static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, ConstExprValue *const_val) {5823static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, ConstExprValue *const_val) {
5826 switch (const_val->special) {5824 switch (const_val->special) {
5827 case ConstValSpecialLazy:
5828 case ConstValSpecialRuntime:5825 case ConstValSpecialRuntime:
5829 zig_unreachable();5826 zig_unreachable();
5830 case ConstValSpecialUndef:5827 case ConstValSpecialUndef:
...@@ -6082,7 +6079,6 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c...@@ -6082,7 +6079,6 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
6082 assert(type_has_bits(type_entry));6079 assert(type_has_bits(type_entry));
60836080
6084 switch (const_val->special) {6081 switch (const_val->special) {
6085 case ConstValSpecialLazy:
6086 case ConstValSpecialRuntime:6082 case ConstValSpecialRuntime:
6087 zig_unreachable();6083 zig_unreachable();
6088 case ConstValSpecialUndef:6084 case ConstValSpecialUndef:
...@@ -8249,7 +8245,7 @@ static void gen_root_source(CodeGen *g) {...@@ -8249,7 +8245,7 @@ static void gen_root_source(CodeGen *g) {
8249 }8245 }
8250 Tld *panic_tld = find_decl(g, &get_container_scope(import_with_panic)->base, buf_create_from_str("panic"));8246 Tld *panic_tld = find_decl(g, &get_container_scope(import_with_panic)->base, buf_create_from_str("panic"));
8251 assert(panic_tld != nullptr);8247 assert(panic_tld != nullptr);
8252 resolve_top_level_decl(g, panic_tld, nullptr, false);8248 resolve_top_level_decl(g, panic_tld, nullptr);
8253 }8249 }
82548250
82558251
src/ir.cpp+132-304
...@@ -154,7 +154,6 @@ struct ConstCastBadAllowsZero {...@@ -154,7 +154,6 @@ struct ConstCastBadAllowsZero {
154enum UndefAllowed {154enum UndefAllowed {
155 UndefOk,155 UndefOk,
156 UndefBad,156 UndefBad,
157 LazyOk,
158};157};
159158
160static IrInstruction *ir_gen_node(IrBuilder *irb, AstNode *node, Scope *scope);159static IrInstruction *ir_gen_node(IrBuilder *irb, AstNode *node, Scope *scope);
...@@ -10257,57 +10256,32 @@ static IrInstruction *ir_get_const_ptr(IrAnalyze *ira, IrInstruction *instructio...@@ -10257,57 +10256,32 @@ static IrInstruction *ir_get_const_ptr(IrAnalyze *ira, IrInstruction *instructio
10257 return const_instr;10256 return const_instr;
10258}10257}
1025910258
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
10294static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, UndefAllowed undef_allowed) {10259static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, UndefAllowed undef_allowed) {
10295 Error err;10260 switch (value->value.special) {
10296 if ((err = ir_resolve_const_val(ira->codegen, ira->new_irb.exec, value->source_node,10261 case ConstValSpecialStatic:
10297 &value->value, undef_allowed)))10262 return &value->value;
10298 {10263 case ConstValSpecialRuntime:
10299 return nullptr;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 }
10300 }10276 }
10301 return &value->value;10277 zig_unreachable();
10302}10278}
1030310279
10304ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,10280ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
10305 ZigType *expected_type, size_t *backward_branch_count, size_t backward_branch_quota,10281 ZigType *expected_type, size_t *backward_branch_count, size_t backward_branch_quota,
10306 ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,10282 ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,
10307 IrExecutable *parent_exec, AstNode *expected_type_source_node, bool allow_lazy)10283 IrExecutable *parent_exec, AstNode *expected_type_source_node)
10308{10284{
10309 Error err;
10310
10311 if (expected_type != nullptr && type_is_invalid(expected_type))10285 if (expected_type != nullptr && type_is_invalid(expected_type))
10312 return &codegen->invalid_instruction->value;10286 return &codegen->invalid_instruction->value;
1031310287
...@@ -10352,24 +10326,7 @@ ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *nod...@@ -10352,24 +10326,7 @@ ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *nod
10352 fprintf(stderr, "}\n");10326 fprintf(stderr, "}\n");
10353 }10327 }
1035410328
10355 ConstExprValue *result = ir_exec_const_result(codegen, analyzed_executable);10329 return 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;
10373}10330}
1037410331
10375static ZigType *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {10332static ZigType *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {
...@@ -10382,7 +10339,12 @@ static ZigType *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {...@@ -10382,7 +10339,12 @@ static ZigType *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {
10382 return ira->codegen->builtin_types.entry_invalid;10339 return ira->codegen->builtin_types.entry_invalid;
10383 }10340 }
1038410341
10385 return ir_resolve_const_type(ira->codegen, ira->new_irb.exec, type_value->source_node, &type_value->value);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;
10386}10348}
1038710349
10388static ZigType *ir_resolve_error_set_type(IrAnalyze *ira, IrInstruction *op_source, IrInstruction *type_value) {10350static ZigType *ir_resolve_error_set_type(IrAnalyze *ira, IrInstruction *op_source, IrInstruction *type_value) {
...@@ -11873,38 +11835,33 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc...@@ -11873,38 +11835,33 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc
11873 }11835 }
11874}11836}
1187511837
11876static bool ir_resolve_const_align(CodeGen *codegen, IrExecutable *exec, AstNode *source_node,11838static bool ir_resolve_align(IrAnalyze *ira, IrInstruction *value, uint32_t *out) {
11877 ConstExprValue *const_val, uint32_t *out)11839 if (type_is_invalid(value->value.type))
11878{11840 return false;
11879 Error err;11841
11880 if ((err = ir_resolve_const_val(codegen, exec, source_node, const_val, UndefBad)))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)
11881 return false;11848 return false;
1188211849
11883 uint32_t align_bytes = bigint_as_unsigned(&const_val->data.x_bigint);11850 uint32_t align_bytes = bigint_as_unsigned(&const_val->data.x_bigint);
11884 if (align_bytes == 0) {11851 if (align_bytes == 0) {
11885 exec_add_error_node(codegen, exec, source_node, buf_sprintf("alignment must be >= 1"));11852 ir_add_error(ira, value, buf_sprintf("alignment must be >= 1"));
11886 return false;11853 return false;
11887 }11854 }
1188811855
11889 if (!is_power_of_2(align_bytes)) {11856 if (!is_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));11857 ir_add_error(ira, value, buf_sprintf("alignment value %" PRIu32 " is not a power of 2", align_bytes));
11891 return false;11858 return false;
11892 }11859 }
11860
11893 *out = align_bytes;11861 *out = align_bytes;
11894 return true;11862 return true;
11895}11863}
1189611864
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
11908static bool ir_resolve_unsigned(IrAnalyze *ira, IrInstruction *value, ZigType *int_type, uint64_t *out) {11865static bool ir_resolve_unsigned(IrAnalyze *ira, IrInstruction *value, ZigType *int_type, uint64_t *out) {
11909 if (type_is_invalid(value->value.type))11866 if (type_is_invalid(value->value.type))
11910 return false;11867 return false;
...@@ -12072,140 +12029,6 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) {...@@ -12072,140 +12029,6 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) {
12072 return result;12029 return result;
12073}12030}
1207412031
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
12209static IrInstruction *ir_analyze_instruction_add_implicit_return_type(IrAnalyze *ira,12032static IrInstruction *ir_analyze_instruction_add_implicit_return_type(IrAnalyze *ira,
12210 IrInstructionAddImplicitReturnType *instruction)12033 IrInstructionAddImplicitReturnType *instruction)
12211{12034{
...@@ -14179,7 +14002,7 @@ static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction,...@@ -14179,7 +14002,7 @@ static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction,
14179 if (linkage_makes_it_runtime)14002 if (linkage_makes_it_runtime)
14180 goto no_mem_slot;14003 goto no_mem_slot;
1418114004
14182 if (value_is_comptime(var->const_value)) {14005 if (var->const_value->special == ConstValSpecialStatic) {
14183 mem_slot = var->const_value;14006 mem_slot = var->const_value;
14184 } else {14007 } else {
14185 if (var->mem_slot_index != SIZE_MAX && (comptime_var_mem || var->gen_is_const)) {14008 if (var->mem_slot_index != SIZE_MAX && (comptime_var_mem || var->gen_is_const)) {
...@@ -14197,7 +14020,6 @@ static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction,...@@ -14197,7 +14020,6 @@ static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction,
14197 case ConstValSpecialRuntime:14020 case ConstValSpecialRuntime:
14198 goto no_mem_slot;14021 goto no_mem_slot;
14199 case ConstValSpecialStatic: // fallthrough14022 case ConstValSpecialStatic: // fallthrough
14200 case ConstValSpecialLazy: // fallthrough
14201 case ConstValSpecialUndef: {14023 case ConstValSpecialUndef: {
14202 ConstPtrMut ptr_mut;14024 ConstPtrMut ptr_mut;
14203 if (comptime_var_mem) {14025 if (comptime_var_mem) {
...@@ -14478,7 +14300,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call...@@ -14478,7 +14300,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call
14478 AstNode *body_node = fn_entry->body_node;14300 AstNode *body_node = fn_entry->body_node;
14479 result = ir_eval_const_value(ira->codegen, exec_scope, body_node, return_type,14301 result = ir_eval_const_value(ira->codegen, exec_scope, body_node, return_type,
14480 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, fn_entry,14302 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, fn_entry,
14481 nullptr, call_instruction->base.source_node, nullptr, ira->new_irb.exec, return_type_node, false);14303 nullptr, call_instruction->base.source_node, nullptr, ira->new_irb.exec, return_type_node);
1448214304
14483 if (inferred_err_set_type != nullptr) {14305 if (inferred_err_set_type != nullptr) {
14484 inferred_err_set_type->data.error_set.infer_fn = nullptr;14306 inferred_err_set_type->data.error_set.infer_fn = nullptr;
...@@ -14674,8 +14496,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call...@@ -14674,8 +14496,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call
14674 ConstExprValue *align_result = ir_eval_const_value(ira->codegen, impl_fn->child_scope,14496 ConstExprValue *align_result = ir_eval_const_value(ira->codegen, impl_fn->child_scope,
14675 fn_proto_node->data.fn_proto.align_expr, get_align_amt_type(ira->codegen),14497 fn_proto_node->data.fn_proto.align_expr, get_align_amt_type(ira->codegen),
14676 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota,14498 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota,
14677 nullptr, nullptr, fn_proto_node->data.fn_proto.align_expr, nullptr, ira->new_irb.exec, nullptr,14499 nullptr, nullptr, fn_proto_node->data.fn_proto.align_expr, nullptr, ira->new_irb.exec, nullptr);
14678 false);
14679 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(&ira->new_irb,14500 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(&ira->new_irb,
14680 impl_fn->child_scope, fn_proto_node->data.fn_proto.align_expr);14501 impl_fn->child_scope, fn_proto_node->data.fn_proto.align_expr);
14681 const_instruction->base.value = *align_result;14502 const_instruction->base.value = *align_result;
...@@ -15808,7 +15629,7 @@ static IrInstruction *ir_analyze_container_member_access_inner(IrAnalyze *ira,...@@ -15808,7 +15629,7 @@ static IrInstruction *ir_analyze_container_member_access_inner(IrAnalyze *ira,
15808 auto entry = container_scope->decl_table.maybe_get(field_name);15629 auto entry = container_scope->decl_table.maybe_get(field_name);
15809 Tld *tld = entry ? entry->value : nullptr;15630 Tld *tld = entry ? entry->value : nullptr;
15810 if (tld && tld->id == TldIdFn) {15631 if (tld && tld->id == TldIdFn) {
15811 resolve_top_level_decl(ira->codegen, tld, source_instr->source_node, false);15632 resolve_top_level_decl(ira->codegen, tld, source_instr->source_node);
15812 if (tld->resolution == TldResolutionInvalid)15633 if (tld->resolution == TldResolutionInvalid)
15813 return ira->codegen->invalid_instruction;15634 return ira->codegen->invalid_instruction;
15814 TldFn *tld_fn = (TldFn *)tld;15635 TldFn *tld_fn = (TldFn *)tld;
...@@ -15999,7 +15820,7 @@ static void add_link_lib_symbol(IrAnalyze *ira, Buf *lib_name, Buf *symbol_name,...@@ -15999,7 +15820,7 @@ static void add_link_lib_symbol(IrAnalyze *ira, Buf *lib_name, Buf *symbol_name,
1599915820
1600015821
16001static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_instruction, Tld *tld) {15822static IrInstruction *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source_instruction, Tld *tld) {
16002 resolve_top_level_decl(ira->codegen, tld, source_instruction->source_node, false);15823 resolve_top_level_decl(ira->codegen, tld, source_instruction->source_node);
16003 if (tld->resolution == TldResolutionInvalid)15824 if (tld->resolution == TldResolutionInvalid)
16004 return ira->codegen->invalid_instruction;15825 return ira->codegen->invalid_instruction;
1600515826
...@@ -16655,29 +16476,22 @@ static IrInstruction *ir_analyze_instruction_set_float_mode(IrAnalyze *ira,...@@ -16655,29 +16476,22 @@ static IrInstruction *ir_analyze_instruction_set_float_mode(IrAnalyze *ira,
16655static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,16476static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
16656 IrInstructionSliceType *slice_type_instruction)16477 IrInstructionSliceType *slice_type_instruction)
16657{16478{
16658 IrInstruction *result = ir_const(ira, &slice_type_instruction->base, ira->codegen->builtin_types.entry_type);16479 Error err;
16659 result->value.special = ConstValSpecialLazy;16480 uint32_t align_bytes = 0;
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
16666 if (slice_type_instruction->align_value != nullptr) {16481 if (slice_type_instruction->align_value != nullptr) {
16667 lazy_slice_type->align_val = ir_resolve_const(ira, slice_type_instruction->align_value->child, LazyOk);16482 if (!ir_resolve_align(ira, slice_type_instruction->align_value->child, &align_bytes))
16668 if (lazy_slice_type->align_val == nullptr)
16669 return ira->codegen->invalid_instruction;16483 return ira->codegen->invalid_instruction;
16670 }16484 }
1667116485
16672 lazy_slice_type->elem_type = ir_resolve_type(ira, slice_type_instruction->child_type->child);16486 ZigType *child_type = ir_resolve_type(ira, slice_type_instruction->child_type->child);
16673 if (type_is_invalid(lazy_slice_type->elem_type))16487 if (type_is_invalid(child_type))
16674 return ira->codegen->invalid_instruction;16488 return ira->codegen->invalid_instruction;
1667516489
16676 lazy_slice_type->is_const = slice_type_instruction->is_const;16490 bool is_const = slice_type_instruction->is_const;
16677 lazy_slice_type->is_volatile = slice_type_instruction->is_volatile;16491 bool is_volatile = slice_type_instruction->is_volatile;
16678 lazy_slice_type->is_allowzero = slice_type_instruction->is_allow_zero;16492 bool is_allow_zero = slice_type_instruction->is_allow_zero;
1667916493
16680 switch (lazy_slice_type->elem_type->id) {16494 switch (child_type->id) {
16681 case ZigTypeIdInvalid: // handled above16495 case ZigTypeIdInvalid: // handled above
16682 zig_unreachable();16496 zig_unreachable();
16683 case ZigTypeIdUnreachable:16497 case ZigTypeIdUnreachable:
...@@ -16686,7 +16500,7 @@ static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,...@@ -16686,7 +16500,7 @@ static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
16686 case ZigTypeIdArgTuple:16500 case ZigTypeIdArgTuple:
16687 case ZigTypeIdOpaque:16501 case ZigTypeIdOpaque:
16688 ir_add_error_node(ira, slice_type_instruction->base.source_node,16502 ir_add_error_node(ira, slice_type_instruction->base.source_node,
16689 buf_sprintf("slice of type '%s' not allowed", buf_ptr(&lazy_slice_type->elem_type->name)));16503 buf_sprintf("slice of type '%s' not allowed", buf_ptr(&child_type->name)));
16690 return ira->codegen->invalid_instruction;16504 return ira->codegen->invalid_instruction;
16691 case ZigTypeIdMetaType:16505 case ZigTypeIdMetaType:
16692 case ZigTypeIdVoid:16506 case ZigTypeIdVoid:
...@@ -16708,7 +16522,14 @@ static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,...@@ -16708,7 +16522,14 @@ static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
16708 case ZigTypeIdBoundFn:16522 case ZigTypeIdBoundFn:
16709 case ZigTypeIdPromise:16523 case ZigTypeIdPromise:
16710 case ZigTypeIdVector:16524 case ZigTypeIdVector:
16711 return result;16525 {
16526 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusZeroBitsKnown)))
16527 return ira->codegen->invalid_instruction;
16528 ZigType *slice_ptr_type = get_pointer_to_type_extra(ira->codegen, child_type,
16529 is_const, is_volatile, PtrLenUnknown, align_bytes, 0, 0, is_allow_zero);
16530 ZigType *result_type = get_slice_type(ira->codegen, slice_ptr_type);
16531 return ir_const_type(ira, &slice_type_instruction->base, result_type);
16532 }
16712 }16533 }
16713 zig_unreachable();16534 zig_unreachable();
16714}16535}
...@@ -16815,7 +16636,7 @@ static IrInstruction *ir_analyze_instruction_array_type(IrAnalyze *ira,...@@ -16815,7 +16636,7 @@ static IrInstruction *ir_analyze_instruction_array_type(IrAnalyze *ira,
16815 case ZigTypeIdPromise:16636 case ZigTypeIdPromise:
16816 case ZigTypeIdVector:16637 case ZigTypeIdVector:
16817 {16638 {
16818 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusSizeKnown)))16639 if ((err = ensure_complete_type(ira->codegen, child_type)))
16819 return ira->codegen->invalid_instruction;16640 return ira->codegen->invalid_instruction;
16820 ZigType *result_type = get_array_type(ira->codegen, child_type, size);16641 ZigType *result_type = get_array_type(ira->codegen, child_type, size);
16821 return ir_const_type(ira, &array_type_instruction->base, result_type);16642 return ir_const_type(ira, &array_type_instruction->base, result_type);
...@@ -18172,7 +17993,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,...@@ -18172,7 +17993,7 @@ static Error ir_make_type_info_defs(IrAnalyze *ira, IrInstruction *source_instr,
18172 while ((curr_entry = decl_it.next()) != nullptr) {17993 while ((curr_entry = decl_it.next()) != nullptr) {
18173 // If the definition is unresolved, force it to be resolved again.17994 // If the definition is unresolved, force it to be resolved again.
18174 if (curr_entry->value->resolution == TldResolutionUnresolved) {17995 if (curr_entry->value->resolution == TldResolutionUnresolved) {
18175 resolve_top_level_decl(ira->codegen, curr_entry->value, curr_entry->value->source_node, false);17996 resolve_top_level_decl(ira->codegen, curr_entry->value, curr_entry->value->source_node);
18176 if (curr_entry->value->resolution != TldResolutionOk) {17997 if (curr_entry->value->resolution != TldResolutionOk) {
18177 return ErrorSemanticAnalyzeFail;17998 return ErrorSemanticAnalyzeFail;
18178 }17999 }
...@@ -19161,7 +18982,7 @@ static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruct...@@ -19161,7 +18982,7 @@ static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruct
19161 ZigType *void_type = ira->codegen->builtin_types.entry_void;18982 ZigType *void_type = ira->codegen->builtin_types.entry_void;
19162 ConstExprValue *cimport_result = ir_eval_const_value(ira->codegen, &cimport_scope->base, block_node, void_type,18983 ConstExprValue *cimport_result = ir_eval_const_value(ira->codegen, &cimport_scope->base, block_node, void_type,
19163 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, nullptr,18984 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, nullptr,
19164 &cimport_scope->buf, block_node, nullptr, nullptr, nullptr, false);18985 &cimport_scope->buf, block_node, nullptr, nullptr, nullptr);
19165 if (type_is_invalid(cimport_result->type))18986 if (type_is_invalid(cimport_result->type))
19166 return ira->codegen->invalid_instruction;18987 return ira->codegen->invalid_instruction;
1916718988
...@@ -20708,11 +20529,15 @@ static IrInstruction *ir_analyze_instruction_handle(IrAnalyze *ira, IrInstructio...@@ -20708,11 +20529,15 @@ static IrInstruction *ir_analyze_instruction_handle(IrAnalyze *ira, IrInstructio
20708}20529}
2070920530
20710static IrInstruction *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstructionAlignOf *instruction) {20531static IrInstruction *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstructionAlignOf *instruction) {
20532 Error err;
20711 IrInstruction *type_value = instruction->type_value->child;20533 IrInstruction *type_value = instruction->type_value->child;
20712 if (type_is_invalid(type_value->value.type))20534 if (type_is_invalid(type_value->value.type))
20713 return ira->codegen->invalid_instruction;20535 return ira->codegen->invalid_instruction;
20714 ZigType *type_entry = ir_resolve_type(ira, type_value);20536 ZigType *type_entry = ir_resolve_type(ira, type_value);
2071520537
20538 if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusAlignmentKnown)))
20539 return ira->codegen->invalid_instruction;
20540
20716 switch (type_entry->id) {20541 switch (type_entry->id) {
20717 case ZigTypeIdInvalid:20542 case ZigTypeIdInvalid:
20718 zig_unreachable();20543 zig_unreachable();
...@@ -20744,25 +20569,12 @@ static IrInstruction *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstruct...@@ -20744,25 +20569,12 @@ static IrInstruction *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstruct
20744 case ZigTypeIdUnion:20569 case ZigTypeIdUnion:
20745 case ZigTypeIdFn:20570 case ZigTypeIdFn:
20746 case ZigTypeIdVector:20571 case ZigTypeIdVector:
20747 break;20572 {
20573 uint64_t align_in_bytes = get_abi_alignment(ira->codegen, type_entry);
20574 return ir_const_unsigned(ira, &instruction->base, align_in_bytes);
20575 }
20748 }20576 }
20749 if (type_is_resolved(type_entry, ResolveStatusAlignmentKnown)) {20577 zig_unreachable();
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;
20766}20578}
2076720579
20768static IrInstruction *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInstructionOverflowOp *instruction) {20580static IrInstruction *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInstructionOverflowOp *instruction) {
...@@ -21017,77 +20829,96 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct...@@ -21017,77 +20829,96 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct
21017 AstNode *proto_node = instruction->base.source_node;20829 AstNode *proto_node = instruction->base.source_node;
21018 assert(proto_node->type == NodeTypeFnProto);20830 assert(proto_node->type == NodeTypeFnProto);
2101920831
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
21028 if (proto_node->data.fn_proto.auto_err_set) {20832 if (proto_node->data.fn_proto.auto_err_set) {
21029 ir_add_error(ira, &instruction->base,20833 ir_add_error(ira, &instruction->base,
21030 buf_sprintf("inferring error set of return type valid only for function definitions"));20834 buf_sprintf("inferring error set of return type valid only for function definitions"));
21031 return ira->codegen->invalid_instruction;20835 return ira->codegen->invalid_instruction;
21032 }20836 }
2103320837
21034 size_t param_count = proto_node->data.fn_proto.params.length;20838 FnTypeId fn_type_id = {0};
21035 lazy_fn_type->proto_node = proto_node;20839 init_fn_type_id(&fn_type_id, proto_node, proto_node->data.fn_proto.params.length);
21036 lazy_fn_type->param_types = allocate<ConstExprValue *>(param_count);
2103720840
21038 for (size_t i = 0; i < param_count; i += 1) {20841 for (; fn_type_id.next_param_index < fn_type_id.param_count; fn_type_id.next_param_index += 1) {
21039 AstNode *param_node = proto_node->data.fn_proto.params.at(i);20842 AstNode *param_node = proto_node->data.fn_proto.params.at(fn_type_id.next_param_index);
21040 assert(param_node->type == NodeTypeParamDecl);20843 assert(param_node->type == NodeTypeParamDecl);
2104120844
21042 bool param_is_var_args = param_node->data.param_decl.is_var_args;20845 bool param_is_var_args = param_node->data.param_decl.is_var_args;
21043 lazy_fn_type->is_var_args = true;
21044 if (param_is_var_args) {20846 if (param_is_var_args) {
21045 if (proto_node->data.fn_proto.cc == CallingConventionC) {20847 if (fn_type_id.cc == CallingConventionC) {
21046 break;20848 fn_type_id.param_count = fn_type_id.next_param_index;
21047 } else if (proto_node->data.fn_proto.cc == CallingConventionUnspecified) {20849 continue;
21048 lazy_fn_type->is_generic = true;20850 } else if (fn_type_id.cc == CallingConventionUnspecified) {
21049 return result;20851 return ir_const_type(ira, &instruction->base, get_generic_fn_type(ira->codegen, &fn_type_id));
21050 } else {20852 } else {
21051 zig_unreachable();20853 zig_unreachable();
21052 }20854 }
21053 }20855 }
20856 FnTypeParamInfo *param_info = &fn_type_id.param_info[fn_type_id.next_param_index];
20857 param_info->is_noalias = param_node->data.param_decl.is_noalias;
2105420858
21055 if (instruction->param_types[i] == nullptr) {20859 if (instruction->param_types[fn_type_id.next_param_index] == nullptr) {
21056 lazy_fn_type->is_generic = true;20860 param_info->type = nullptr;
21057 return result;20861 return ir_const_type(ira, &instruction->base, get_generic_fn_type(ira->codegen, &fn_type_id));
20862 } else {
20863 IrInstruction *param_type_value = instruction->param_types[fn_type_id.next_param_index]->child;
20864 if (type_is_invalid(param_type_value->value.type))
20865 return ira->codegen->invalid_instruction;
20866 ZigType *param_type = ir_resolve_type(ira, param_type_value);
20867 switch (type_requires_comptime(ira->codegen, param_type)) {
20868 case ReqCompTimeYes:
20869 if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
20870 ir_add_error(ira, param_type_value,
20871 buf_sprintf("parameter of type '%s' not allowed in function with calling convention '%s'",
20872 buf_ptr(&param_type->name), calling_convention_name(fn_type_id.cc)));
20873 return ira->codegen->invalid_instruction;
20874 }
20875 param_info->type = param_type;
20876 fn_type_id.next_param_index += 1;
20877 return ir_const_type(ira, &instruction->base, get_generic_fn_type(ira->codegen, &fn_type_id));
20878 case ReqCompTimeInvalid:
20879 return ira->codegen->invalid_instruction;
20880 case ReqCompTimeNo:
20881 break;
20882 }
20883 if (!type_has_bits(param_type) && !calling_convention_allows_zig_types(fn_type_id.cc)) {
20884 ir_add_error(ira, param_type_value,
20885 buf_sprintf("parameter of type '%s' has 0 bits; not allowed in function with calling convention '%s'",
20886 buf_ptr(&param_type->name), calling_convention_name(fn_type_id.cc)));
20887 return ira->codegen->invalid_instruction;
20888 }
20889 param_info->type = param_type;
21058 }20890 }
2105920891
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;
21067 }20892 }
2106820893
21069 if (instruction->align_value != nullptr) {20894 if (instruction->align_value != nullptr) {
21070 lazy_fn_type->align_val = ir_resolve_const(ira, instruction->align_value->child, LazyOk);20895 if (!ir_resolve_align(ira, instruction->align_value->child, &fn_type_id.alignment))
21071 if (lazy_fn_type->align_val == nullptr)
21072 return ira->codegen->invalid_instruction;20896 return ira->codegen->invalid_instruction;
21073 }20897 }
2107420898
21075 lazy_fn_type->return_type = ir_resolve_const(ira, instruction->return_type->child, LazyOk);20899 IrInstruction *return_type_value = instruction->return_type->child;
21076 if (lazy_fn_type->return_type == nullptr)20900 fn_type_id.return_type = ir_resolve_type(ira, return_type_value);
20901 if (type_is_invalid(fn_type_id.return_type))
21077 return ira->codegen->invalid_instruction;20902 return ira->codegen->invalid_instruction;
20903 if (fn_type_id.return_type->id == ZigTypeIdOpaque) {
20904 ir_add_error(ira, instruction->return_type,
20905 buf_sprintf("return type cannot be opaque"));
20906 return ira->codegen->invalid_instruction;
20907 }
2107820908
21079 if (proto_node->data.fn_proto.cc == CallingConventionAsync) {20909 if (fn_type_id.cc == CallingConventionAsync) {
21080 if (instruction->async_allocator_type_value == nullptr) {20910 if (instruction->async_allocator_type_value == nullptr) {
21081 ir_add_error(ira, &instruction->base,20911 ir_add_error(ira, &instruction->base,
21082 buf_sprintf("async fn proto missing allocator type"));20912 buf_sprintf("async fn proto missing allocator type"));
21083 return ira->codegen->invalid_instruction;20913 return ira->codegen->invalid_instruction;
21084 }20914 }
21085 lazy_fn_type->async_allocator_type = ir_resolve_const(ira, instruction->async_allocator_type_value->child, LazyOk);20915 IrInstruction *async_allocator_type_value = instruction->async_allocator_type_value->child;
21086 if (lazy_fn_type->async_allocator_type == nullptr)20916 fn_type_id.async_allocator_type = ir_resolve_type(ira, async_allocator_type_value);
20917 if (type_is_invalid(fn_type_id.async_allocator_type))
21087 return ira->codegen->invalid_instruction;20918 return ira->codegen->invalid_instruction;
21088 }20919 }
2108920920
21090 return result;20921 return ir_const_type(ira, &instruction->base, get_fn_type(ira->codegen, &fn_type_id));
21091}20922}
2109220923
21093static IrInstruction *ir_analyze_instruction_test_comptime(IrAnalyze *ira, IrInstructionTestComptime *instruction) {20924static IrInstruction *ir_analyze_instruction_test_comptime(IrAnalyze *ira, IrInstructionTestComptime *instruction) {
...@@ -21756,11 +21587,8 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou...@@ -21756,11 +21587,8 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
21756 val->type->data.vector.len);21587 val->type->data.vector.len);
21757 case ZigTypeIdEnum:21588 case ZigTypeIdEnum:
21758 switch (val->type->data.enumeration.layout) {21589 switch (val->type->data.enumeration.layout) {
21759 case ContainerLayoutAuto: {21590 case ContainerLayoutAuto:
21760 opt_ir_add_error_node(ira, codegen, source_node,21591 zig_panic("TODO buf_read_value_bytes enum auto");
21761 buf_sprintf("compiler bug: TODO: implement enum byte reinterpretation"));
21762 return ErrorSemanticAnalyzeFail;
21763 }
21764 case ContainerLayoutPacked:21592 case ContainerLayoutPacked:
21765 zig_panic("TODO buf_read_value_bytes enum packed");21593 zig_panic("TODO buf_read_value_bytes enum packed");
21766 case ContainerLayoutExtern: {21594 case ContainerLayoutExtern: {
...@@ -22056,7 +21884,7 @@ static IrInstruction *ir_analyze_instruction_decl_ref(IrAnalyze *ira,...@@ -22056,7 +21884,7 @@ static IrInstruction *ir_analyze_instruction_decl_ref(IrAnalyze *ira,
22056 Tld *tld = instruction->tld;21884 Tld *tld = instruction->tld;
22057 LVal lval = instruction->lval;21885 LVal lval = instruction->lval;
2205821886
22059 resolve_top_level_decl(ira->codegen, tld, instruction->base.source_node, true);21887 resolve_top_level_decl(ira->codegen, tld, instruction->base.source_node);
22060 if (tld->resolution == TldResolutionInvalid)21888 if (tld->resolution == TldResolutionInvalid)
22061 return ira->codegen->invalid_instruction;21889 return ira->codegen->invalid_instruction;
2206221890
src/ir.hpp+1-2
...@@ -16,8 +16,7 @@ bool ir_gen_fn(CodeGen *g, ZigFn *fn_entry);...@@ -16,8 +16,7 @@ bool ir_gen_fn(CodeGen *g, ZigFn *fn_entry);
16ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,16ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
17 ZigType *expected_type, size_t *backward_branch_count, size_t backward_branch_quota,17 ZigType *expected_type, size_t *backward_branch_count, size_t backward_branch_quota,
18 ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,18 ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,
19 IrExecutable *parent_exec, AstNode *expected_type_source_node, bool allow_lazy);19 IrExecutable *parent_exec, AstNode *expected_type_source_node);
20Error ir_resolve_lazy(CodeGen *codegen, AstNode *source_node, ConstExprValue *val);
2120
22ZigType *ir_analyze(CodeGen *g, IrExecutable *old_executable, IrExecutable *new_executable,21ZigType *ir_analyze(CodeGen *g, IrExecutable *old_executable, IrExecutable *new_executable,
23 ZigType *expected_type, AstNode *expected_type_source_node);22 ZigType *expected_type, AstNode *expected_type_source_node);