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
signature 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 {
256256 ConstValSpecialRuntime,
257257 ConstValSpecialStatic,
258258 ConstValSpecialUndef,
259 ConstValSpecialLazy,
260259};
261260
262261enum RuntimeHintErrorUnion {
......@@ -292,43 +291,6 @@ struct ConstGlobalRefs {
292291 LLVMValueRef llvm_global;
293292};
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
332294struct ConstExprValue {
333295 ZigType *type;
334296 ConstValSpecial special;
......@@ -356,7 +318,6 @@ struct ConstExprValue {
356318 ConstPtrValue x_ptr;
357319 ConstArgTuple x_arg_tuple;
358320 Buf *x_enum_literal;
359 LazyValue *x_lazy;
360321
361322 // populated if special == ConstValSpecialRuntime
362323 RuntimeHintErrorUnion rh_error_union;
......@@ -398,7 +359,6 @@ enum TldResolution {
398359 TldResolutionUnresolved,
399360 TldResolutionResolving,
400361 TldResolutionInvalid,
401 TldResolutionOkLazy,
402362 TldResolutionOk,
403363};
404364
......@@ -1104,8 +1064,7 @@ struct ZigTypeArray {
11041064
11051065struct TypeStructField {
11061066 Buf *name;
1107 ZigType *type_entry; // available after ResolveStatusSizeKnown
1108 ConstExprValue *type_val; // available after ResolveStatusZeroBitsKnown
1067 ZigType *type_entry;
11091068 size_t src_index;
11101069 size_t gen_index;
11111070 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) {
902902 }
903903 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.
905906 fn_type->size_in_bits = g->builtin_types.entry_usize->size_in_bits;
906907 fn_type->abi_size = g->builtin_types.entry_usize->abi_size;
907 // see also type_val_resolve_abi_align
908 fn_type->abi_align = (fn_type_id->alignment == 0) ? 1 : fn_type_id->alignment;
908 fn_type->abi_align = g->builtin_types.entry_usize->abi_align;
909909
910910 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
963963 return entry;
964964}
965965
966static ConstExprValue *analyze_const_value_allow_lazy(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry,
967 Buf *type_name, bool allow_lazy)
966static ConstExprValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, ZigType *type_entry,
967 Buf *type_name)
968968{
969969 size_t backward_branch_count = 0;
970970 return ir_eval_const_value(g, scope, node, type_entry,
971971 &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);
973973}
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
1094975ZigType *analyze_type_expr(CodeGen *g, Scope *scope, AstNode *node) {
1095976 ConstExprValue *result = analyze_const_value(g, scope, node, g->builtin_types.entry_type, nullptr);
1096977 if (type_is_invalid(result->type))
......@@ -1656,9 +1537,17 @@ ZigType *get_struct_type(CodeGen *g, const char *type_name, const char *field_na
16561537 size_t next_offset = 0;
16571538 for (size_t i = 0; i < field_count; i += 1) {
16581539 TypeStructField *field = &struct_type->data.structure.fields[i];
1540 if (field->gen_index == SIZE_MAX)
1541 continue;
16591542 field->offset = next_offset;
1660 size_t next_abi_align = (i + 1 == field_count) ?
1661 abi_align : struct_type->data.structure.fields[i + 1].type_entry->abi_align;
1543 size_t next_src_field_index = i + 1;
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;
16621551 next_offset = next_field_offset(next_offset, abi_align, field->type_entry->abi_size, next_abi_align);
16631552 }
16641553
......@@ -1716,43 +1605,6 @@ static Error resolve_struct_type(CodeGen *g, ZigType *struct_type) {
17161605 size_t size_in_bits = 0;
17171606 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
17561608 // Calculate offsets
17571609 for (size_t i = 0; i < field_count; i += 1) {
17581610 TypeStructField *field = &struct_type->data.structure.fields[i];
......@@ -2186,8 +2038,6 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
21862038static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
21872039 assert(struct_type->id == ZigTypeIdStruct);
21882040
2189 Error err;
2190
21912041 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
21922042 return ErrorSemanticAnalyzeFail;
21932043 if (struct_type->data.structure.resolve_status >= ResolveStatusZeroBitsKnown)
......@@ -2238,36 +2088,29 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
22382088 return ErrorSemanticAnalyzeFail;
22392089 }
22402090
2241 ConstExprValue *field_type_val = analyze_const_value_allow_lazy(g, scope,
2242 field_node->data.struct_field.type, g->builtin_types.entry_type, nullptr, true);
2243 if (type_is_invalid(field_type_val->type)) {
2091 ZigType *field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type);
2092 type_struct_field->type_entry = field_type;
2093 if (type_is_invalid(field_type)) {
22442094 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
22452095 return ErrorSemanticAnalyzeFail;
22462096 }
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
22522097 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
22532098 return ErrorSemanticAnalyzeFail;
22542099
2100 type_struct_field->src_index = i;
2101 type_struct_field->gen_index = SIZE_MAX;
2102
22552103 if (field_node->data.struct_field.value != nullptr) {
22562104 add_node_error(g, field_node->data.struct_field.value,
22572105 buf_sprintf("enums, not structs, support field assignment"));
22582106 }
2259 bool field_is_opaque_type;
2260 if ((err = type_val_resolve_is_opaque_type(g, field_type_val, &field_is_opaque_type))) {
2261 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2262 return ErrorSemanticAnalyzeFail;
2263 }
2264 if (field_is_opaque_type) {
2107 if (field_type->id == ZigTypeIdOpaque) {
22652108 add_node_error(g, field_node->data.struct_field.type,
22662109 buf_sprintf("opaque types have unknown size and therefore cannot be directly embedded in structs"));
22672110 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
22682111 return ErrorSemanticAnalyzeFail;
22692112 }
2270 switch (type_val_resolve_requires_comptime(g, field_type_val)) {
2113 switch (type_requires_comptime(g, field_type)) {
22712114 case ReqCompTimeYes:
22722115 struct_type->data.structure.requires_comptime = true;
22732116 break;
......@@ -2278,12 +2121,7 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
22782121 break;
22792122 }
22802123
2281 bool field_is_zero_bits;
2282 if ((err = type_val_resolve_zero_bits(g, field_type_val, &field_is_zero_bits))) {
2283 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2284 return ErrorSemanticAnalyzeFail;
2285 }
2286 if (field_is_zero_bits)
2124 if (!type_has_bits(field_type))
22872125 continue;
22882126
22892127 type_struct_field->gen_index = gen_field_index;
......@@ -2338,7 +2176,31 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
23382176 bool packed = struct_type->data.structure.layout == ContainerLayoutPacked;
23392177
23402178 for (size_t i = 0; i < field_count; i += 1) {
2179 AstNode *field_source_node = decl_node->data.container_decl.fields.at(i);
23412180 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
23422204 if (field->gen_index == SIZE_MAX)
23432205 continue;
23442206
......@@ -2349,13 +2211,8 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
23492211 }
23502212 } else {
23512213 // TODO: https://github.com/ziglang/zig/issues/1512
2352 size_t field_align;
2353 if ((err = type_val_resolve_abi_align(g, field->type_val, &field_align))) {
2354 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2355 return err;
2356 }
2357 if (field_align > abi_align) {
2358 abi_align = field_align;
2214 if (field_type->abi_align > abi_align) {
2215 abi_align = field_type->abi_align;
23592216 }
23602217 }
23612218 }
......@@ -2993,7 +2850,7 @@ void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source
29932850
29942851void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value) {
29952852 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);
29972854 assert(tld->id == TldIdVar);
29982855 TldVar *tld_var = (TldVar *)tld;
29992856 tld_var->var->const_value = value;
......@@ -3232,7 +3089,7 @@ ZigVar *add_variable(CodeGen *g, AstNode *source_node, Scope *parent_scope, Buf
32323089 return variable_entry;
32333090}
32343091
3235static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) {
3092static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
32363093 AstNode *source_node = tld_var->base.source_node;
32373094 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) {
32733130 if (explicit_type && explicit_type->id == ZigTypeIdInvalid) {
32743131 implicit_type = explicit_type;
32753132 } else if (var_decl->expr) {
3276 init_value = analyze_const_value_allow_lazy(g, tld_var->base.parent_scope, var_decl->expr,
3277 explicit_type, var_decl->symbol, allow_lazy);
3133 init_value = analyze_const_value(g, tld_var->base.parent_scope, var_decl->expr, explicit_type, var_decl->symbol);
32783134 assert(init_value);
32793135 implicit_type = init_value->type;
32803136
......@@ -3337,11 +3193,11 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var, bool allow_lazy) {
33373193 g->global_vars.append(tld_var);
33383194}
33393195
3340void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node, bool allow_lazy) {
3341 bool want_resolve_lazy = tld->resolution == TldResolutionOkLazy && !allow_lazy;
3342 if (tld->resolution != TldResolutionUnresolved && !want_resolve_lazy)
3196void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node) {
3197 if (tld->resolution != TldResolutionUnresolved)
33433198 return;
33443199
3200 assert(tld->resolution != TldResolutionResolving);
33453201 tld->resolution = TldResolutionResolving;
33463202 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
33493205 case TldIdVar:
33503206 {
33513207 TldVar *tld_var = (TldVar *)tld;
3352 if (want_resolve_lazy) {
3353 ir_resolve_lazy(g, source_node, tld_var->var->const_value);
3354 } else {
3355 resolve_decl_var(g, tld_var, allow_lazy);
3356 }
3208 resolve_decl_var(g, tld_var);
33573209 break;
33583210 }
33593211 case TldIdFn:
......@@ -3376,7 +3228,7 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node, bool all
33763228 }
33773229 }
33783230
3379 tld->resolution = allow_lazy ? TldResolutionOkLazy : TldResolutionOk;
3231 tld->resolution = TldResolutionOk;
33803232 g->tld_ref_source_node_stack.pop();
33813233}
33823234
......@@ -4045,7 +3897,7 @@ void semantic_analyze(CodeGen *g) {
40453897 for (; g->resolve_queue_index < g->resolve_queue.length; g->resolve_queue_index += 1) {
40463898 Tld *tld = g->resolve_queue.at(g->resolve_queue_index);
40473899 AstNode *source_node = nullptr;
4048 resolve_top_level_decl(g, tld, source_node, false);
3900 resolve_top_level_decl(g, tld, source_node);
40493901 }
40503902
40513903 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) {
54795331 case ConstValSpecialRuntime:
54805332 buf_appendf(buf, "(runtime value)");
54815333 return;
5482 case ConstValSpecialLazy:
5483 buf_appendf(buf, "(lazy value)");
5484 return;
54855334 case ConstValSpecialUndef:
54865335 buf_appendf(buf, "undefined");
54875336 return;
......@@ -6098,7 +5947,7 @@ bool type_ptr_eql(const ZigType *a, const ZigType *b) {
60985947
60995948ConstExprValue *get_builtin_value(CodeGen *codegen, const char *name) {
61005949 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);
61025951 assert(tld->id == TldIdVar);
61035952 TldVar *tld_var = (TldVar *)tld;
61045953 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
6161ZigVar *find_variable(CodeGen *g, Scope *orig_context, Buf *name, ScopeFnDef **crossed_fndef_scope);
6262Tld *find_decl(CodeGen *g, Scope *scope, Buf *name);
6363Tld *find_container_decl(CodeGen *g, ScopeDecls *decls_scope, Buf *name);
64void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node, bool allow_lazy);
64void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node);
6565
6666ZigType *get_src_ptr_type(ZigType *type);
6767ZigType *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) {
33583358
33593359static bool value_is_all_undef(ConstExprValue *const_val) {
33603360 switch (const_val->special) {
3361 case ConstValSpecialLazy:
3362 zig_unreachable();
33633361 case ConstValSpecialRuntime:
33643362 return false;
33653363 case ConstValSpecialUndef:
......@@ -5824,7 +5822,6 @@ static LLVMValueRef gen_const_ptr_union_recursive(CodeGen *g, ConstExprValue *un
58245822
58255823static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, ConstExprValue *const_val) {
58265824 switch (const_val->special) {
5827 case ConstValSpecialLazy:
58285825 case ConstValSpecialRuntime:
58295826 zig_unreachable();
58305827 case ConstValSpecialUndef:
......@@ -6082,7 +6079,6 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
60826079 assert(type_has_bits(type_entry));
60836080
60846081 switch (const_val->special) {
6085 case ConstValSpecialLazy:
60866082 case ConstValSpecialRuntime:
60876083 zig_unreachable();
60886084 case ConstValSpecialUndef:
......@@ -8249,7 +8245,7 @@ static void gen_root_source(CodeGen *g) {
82498245 }
82508246 Tld *panic_tld = find_decl(g, &get_container_scope(import_with_panic)->base, buf_create_from_str("panic"));
82518247 assert(panic_tld != nullptr);
8252 resolve_top_level_decl(g, panic_tld, nullptr, false);
8248 resolve_top_level_decl(g, panic_tld, nullptr);
82538249 }
82548250
82558251
src/ir.cpp+132-304
......@@ -154,7 +154,6 @@ struct ConstCastBadAllowsZero {
154154enum UndefAllowed {
155155 UndefOk,
156156 UndefBad,
157 LazyOk,
158157};
159158
160159static IrInstruction *ir_gen_node(IrBuilder *irb, AstNode *node, Scope *scope);
......@@ -10257,57 +10256,32 @@ static IrInstruction *ir_get_const_ptr(IrAnalyze *ira, IrInstruction *instructio
1025710256 return const_instr;
1025810257}
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
1029410259static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, UndefAllowed undef_allowed) {
10295 Error err;
10296 if ((err = ir_resolve_const_val(ira->codegen, ira->new_irb.exec, value->source_node,
10297 &value->value, undef_allowed)))
10298 {
10299 return nullptr;
10260 switch (value->value.special) {
10261 case ConstValSpecialStatic:
10262 return &value->value;
10263 case ConstValSpecialRuntime:
10264 if (!type_has_bits(value->value.type)) {
10265 return &value->value;
10266 }
10267 ir_add_error(ira, value, buf_sprintf("unable to evaluate constant expression"));
10268 return nullptr;
10269 case ConstValSpecialUndef:
10270 if (undef_allowed == UndefOk) {
10271 return &value->value;
10272 } else {
10273 ir_add_error(ira, value, buf_sprintf("use of undefined value here causes undefined behavior"));
10274 return nullptr;
10275 }
1030010276 }
10301 return &value->value;
10277 zig_unreachable();
1030210278}
1030310279
1030410280ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
1030510281 ZigType *expected_type, size_t *backward_branch_count, size_t backward_branch_quota,
1030610282 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)
1030810284{
10309 Error err;
10310
1031110285 if (expected_type != nullptr && type_is_invalid(expected_type))
1031210286 return &codegen->invalid_instruction->value;
1031310287
......@@ -10352,24 +10326,7 @@ ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *nod
1035210326 fprintf(stderr, "}\n");
1035310327 }
1035410328
10355 ConstExprValue *result = ir_exec_const_result(codegen, analyzed_executable);
10356
10357 if (!allow_lazy) {
10358 if ((err = ir_resolve_lazy(codegen, node, result)))
10359 return &codegen->invalid_instruction->value;
10360 }
10361 return result;
10362}
10363
10364static ZigType *ir_resolve_const_type(CodeGen *codegen, IrExecutable *exec, AstNode *source_node,
10365 ConstExprValue *val)
10366{
10367 Error err;
10368 if ((err = ir_resolve_const_val(codegen, exec, source_node, val, UndefBad)))
10369 return codegen->builtin_types.entry_invalid;
10370
10371 assert(val->data.x_type != nullptr);
10372 return val->data.x_type;
10329 return ir_exec_const_result(codegen, analyzed_executable);
1037310330}
1037410331
1037510332static ZigType *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {
......@@ -10382,7 +10339,12 @@ static ZigType *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {
1038210339 return ira->codegen->builtin_types.entry_invalid;
1038310340 }
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;
1038610348}
1038710349
1038810350static 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
1187311835 }
1187411836}
1187511837
11876static bool ir_resolve_const_align(CodeGen *codegen, IrExecutable *exec, AstNode *source_node,
11877 ConstExprValue *const_val, uint32_t *out)
11878{
11879 Error err;
11880 if ((err = ir_resolve_const_val(codegen, exec, source_node, const_val, UndefBad)))
11838static bool ir_resolve_align(IrAnalyze *ira, IrInstruction *value, uint32_t *out) {
11839 if (type_is_invalid(value->value.type))
11840 return false;
11841
11842 IrInstruction *casted_value = ir_implicit_cast(ira, value, get_align_amt_type(ira->codegen));
11843 if (type_is_invalid(casted_value->value.type))
11844 return false;
11845
11846 ConstExprValue *const_val = ir_resolve_const(ira, casted_value, UndefBad);
11847 if (!const_val)
1188111848 return false;
1188211849
1188311850 uint32_t align_bytes = bigint_as_unsigned(&const_val->data.x_bigint);
1188411851 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"));
1188611853 return false;
1188711854 }
1188811855
1188911856 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));
1189111858 return false;
1189211859 }
11860
1189311861 *out = align_bytes;
1189411862 return true;
1189511863}
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
1190811865static bool ir_resolve_unsigned(IrAnalyze *ira, IrInstruction *value, ZigType *int_type, uint64_t *out) {
1190911866 if (type_is_invalid(value->value.type))
1191011867 return false;
......@@ -12072,140 +12029,6 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) {
1207212029 return result;
1207312030}
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
1220912032static IrInstruction *ir_analyze_instruction_add_implicit_return_type(IrAnalyze *ira,
1221012033 IrInstructionAddImplicitReturnType *instruction)
1221112034{
......@@ -14179,7 +14002,7 @@ static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction,
1417914002 if (linkage_makes_it_runtime)
1418014003 goto no_mem_slot;
1418114004
14182 if (value_is_comptime(var->const_value)) {
14005 if (var->const_value->special == ConstValSpecialStatic) {
1418314006 mem_slot = var->const_value;
1418414007 } else {
1418514008 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,
1419714020 case ConstValSpecialRuntime:
1419814021 goto no_mem_slot;
1419914022 case ConstValSpecialStatic: // fallthrough
14200 case ConstValSpecialLazy: // fallthrough
1420114023 case ConstValSpecialUndef: {
1420214024 ConstPtrMut ptr_mut;
1420314025 if (comptime_var_mem) {
......@@ -14478,7 +14300,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call
1447814300 AstNode *body_node = fn_entry->body_node;
1447914301 result = ir_eval_const_value(ira->codegen, exec_scope, body_node, return_type,
1448014302 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
1448314305 if (inferred_err_set_type != nullptr) {
1448414306 inferred_err_set_type->data.error_set.infer_fn = nullptr;
......@@ -14674,8 +14496,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call
1467414496 ConstExprValue *align_result = ir_eval_const_value(ira->codegen, impl_fn->child_scope,
1467514497 fn_proto_node->data.fn_proto.align_expr, get_align_amt_type(ira->codegen),
1467614498 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,
14678 false);
14499 nullptr, nullptr, fn_proto_node->data.fn_proto.align_expr, nullptr, ira->new_irb.exec, nullptr);
1467914500 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(&ira->new_irb,
1468014501 impl_fn->child_scope, fn_proto_node->data.fn_proto.align_expr);
1468114502 const_instruction->base.value = *align_result;
......@@ -15808,7 +15629,7 @@ static IrInstruction *ir_analyze_container_member_access_inner(IrAnalyze *ira,
1580815629 auto entry = container_scope->decl_table.maybe_get(field_name);
1580915630 Tld *tld = entry ? entry->value : nullptr;
1581015631 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);
1581215633 if (tld->resolution == TldResolutionInvalid)
1581315634 return ira->codegen->invalid_instruction;
1581415635 TldFn *tld_fn = (TldFn *)tld;
......@@ -15999,7 +15820,7 @@ static void add_link_lib_symbol(IrAnalyze *ira, Buf *lib_name, Buf *symbol_name,
1599915820
1600015821
1600115822static 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);
1600315824 if (tld->resolution == TldResolutionInvalid)
1600415825 return ira->codegen->invalid_instruction;
1600515826
......@@ -16655,29 +16476,22 @@ static IrInstruction *ir_analyze_instruction_set_float_mode(IrAnalyze *ira,
1665516476static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1665616477 IrInstructionSliceType *slice_type_instruction)
1665716478{
16658 IrInstruction *result = ir_const(ira, &slice_type_instruction->base, ira->codegen->builtin_types.entry_type);
16659 result->value.special = ConstValSpecialLazy;
16660
16661 LazyValueSliceType *lazy_slice_type = allocate<LazyValueSliceType>(1);
16662 result->value.data.x_lazy = &lazy_slice_type->base;
16663 lazy_slice_type->base.id = LazyValueIdSliceType;
16664 lazy_slice_type->base.exec = ira->new_irb.exec;
16665
16479 Error err;
16480 uint32_t align_bytes = 0;
1666616481 if (slice_type_instruction->align_value != nullptr) {
16667 lazy_slice_type->align_val = ir_resolve_const(ira, slice_type_instruction->align_value->child, LazyOk);
16668 if (lazy_slice_type->align_val == nullptr)
16482 if (!ir_resolve_align(ira, slice_type_instruction->align_value->child, &align_bytes))
1666916483 return ira->codegen->invalid_instruction;
1667016484 }
1667116485
16672 lazy_slice_type->elem_type = ir_resolve_type(ira, slice_type_instruction->child_type->child);
16673 if (type_is_invalid(lazy_slice_type->elem_type))
16486 ZigType *child_type = ir_resolve_type(ira, slice_type_instruction->child_type->child);
16487 if (type_is_invalid(child_type))
1667416488 return ira->codegen->invalid_instruction;
1667516489
16676 lazy_slice_type->is_const = slice_type_instruction->is_const;
16677 lazy_slice_type->is_volatile = slice_type_instruction->is_volatile;
16678 lazy_slice_type->is_allowzero = slice_type_instruction->is_allow_zero;
16490 bool is_const = slice_type_instruction->is_const;
16491 bool is_volatile = slice_type_instruction->is_volatile;
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) {
1668116495 case ZigTypeIdInvalid: // handled above
1668216496 zig_unreachable();
1668316497 case ZigTypeIdUnreachable:
......@@ -16686,7 +16500,7 @@ static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1668616500 case ZigTypeIdArgTuple:
1668716501 case ZigTypeIdOpaque:
1668816502 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)));
1669016504 return ira->codegen->invalid_instruction;
1669116505 case ZigTypeIdMetaType:
1669216506 case ZigTypeIdVoid:
......@@ -16708,7 +16522,14 @@ static IrInstruction *ir_analyze_instruction_slice_type(IrAnalyze *ira,
1670816522 case ZigTypeIdBoundFn:
1670916523 case ZigTypeIdPromise:
1671016524 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 }
1671216533 }
1671316534 zig_unreachable();
1671416535}
......@@ -16815,7 +16636,7 @@ static IrInstruction *ir_analyze_instruction_array_type(IrAnalyze *ira,
1681516636 case ZigTypeIdPromise:
1681616637 case ZigTypeIdVector:
1681716638 {
16818 if ((err = type_resolve(ira->codegen, child_type, ResolveStatusSizeKnown)))
16639 if ((err = ensure_complete_type(ira->codegen, child_type)))
1681916640 return ira->codegen->invalid_instruction;
1682016641 ZigType *result_type = get_array_type(ira->codegen, child_type, size);
1682116642 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,
1817217993 while ((curr_entry = decl_it.next()) != nullptr) {
1817317994 // If the definition is unresolved, force it to be resolved again.
1817417995 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);
1817617997 if (curr_entry->value->resolution != TldResolutionOk) {
1817717998 return ErrorSemanticAnalyzeFail;
1817817999 }
......@@ -19161,7 +18982,7 @@ static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruct
1916118982 ZigType *void_type = ira->codegen->builtin_types.entry_void;
1916218983 ConstExprValue *cimport_result = ir_eval_const_value(ira->codegen, &cimport_scope->base, block_node, void_type,
1916318984 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);
1916518986 if (type_is_invalid(cimport_result->type))
1916618987 return ira->codegen->invalid_instruction;
1916718988
......@@ -20708,11 +20529,15 @@ static IrInstruction *ir_analyze_instruction_handle(IrAnalyze *ira, IrInstructio
2070820529}
2070920530
2071020531static IrInstruction *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstructionAlignOf *instruction) {
20532 Error err;
2071120533 IrInstruction *type_value = instruction->type_value->child;
2071220534 if (type_is_invalid(type_value->value.type))
2071320535 return ira->codegen->invalid_instruction;
2071420536 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
2071620541 switch (type_entry->id) {
2071720542 case ZigTypeIdInvalid:
2071820543 zig_unreachable();
......@@ -20744,25 +20569,12 @@ static IrInstruction *ir_analyze_instruction_align_of(IrAnalyze *ira, IrInstruct
2074420569 case ZigTypeIdUnion:
2074520570 case ZigTypeIdFn:
2074620571 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 }
2074820576 }
20749 if (type_is_resolved(type_entry, ResolveStatusAlignmentKnown)) {
20750 uint64_t align_in_bytes = get_abi_alignment(ira->codegen, type_entry);
20751 return ir_const_unsigned(ira, &instruction->base, align_in_bytes);
20752 }
20753 // Here we create a lazy value in order to avoid resolving the alignment of the type
20754 // immediately. This avoids false positive dependency loops such as:
20755 // const Node = struct {
20756 // field: []align(@alignOf(Node)) Node,
20757 // };
20758 LazyValueAlignOf *lazy_align_of = allocate<LazyValueAlignOf>(1);
20759 lazy_align_of->base.id = LazyValueIdAlignOf;
20760 lazy_align_of->base.exec = ira->new_irb.exec;
20761 lazy_align_of->target_type = type_entry;
20762 IrInstruction *result = ir_const(ira, &instruction->base, ira->codegen->builtin_types.entry_num_lit_int);
20763 result->value.special = ConstValSpecialLazy;
20764 result->value.data.x_lazy = &lazy_align_of->base;
20765 return result;
20577 zig_unreachable();
2076620578}
2076720579
2076820580static IrInstruction *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInstructionOverflowOp *instruction) {
......@@ -21017,77 +20829,96 @@ static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruct
2101720829 AstNode *proto_node = instruction->base.source_node;
2101820830 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
2102820832 if (proto_node->data.fn_proto.auto_err_set) {
2102920833 ir_add_error(ira, &instruction->base,
2103020834 buf_sprintf("inferring error set of return type valid only for function definitions"));
2103120835 return ira->codegen->invalid_instruction;
2103220836 }
2103320837
21034 size_t param_count = proto_node->data.fn_proto.params.length;
21035 lazy_fn_type->proto_node = proto_node;
21036 lazy_fn_type->param_types = allocate<ConstExprValue *>(param_count);
20838 FnTypeId fn_type_id = {0};
20839 init_fn_type_id(&fn_type_id, proto_node, proto_node->data.fn_proto.params.length);
2103720840
21038 for (size_t i = 0; i < param_count; i += 1) {
21039 AstNode *param_node = proto_node->data.fn_proto.params.at(i);
20841 for (; fn_type_id.next_param_index < fn_type_id.param_count; fn_type_id.next_param_index += 1) {
20842 AstNode *param_node = proto_node->data.fn_proto.params.at(fn_type_id.next_param_index);
2104020843 assert(param_node->type == NodeTypeParamDecl);
2104120844
2104220845 bool param_is_var_args = param_node->data.param_decl.is_var_args;
21043 lazy_fn_type->is_var_args = true;
2104420846 if (param_is_var_args) {
21045 if (proto_node->data.fn_proto.cc == CallingConventionC) {
21046 break;
21047 } else if (proto_node->data.fn_proto.cc == CallingConventionUnspecified) {
21048 lazy_fn_type->is_generic = true;
21049 return result;
20847 if (fn_type_id.cc == CallingConventionC) {
20848 fn_type_id.param_count = fn_type_id.next_param_index;
20849 continue;
20850 } else if (fn_type_id.cc == CallingConventionUnspecified) {
20851 return ir_const_type(ira, &instruction->base, get_generic_fn_type(ira->codegen, &fn_type_id));
2105020852 } else {
2105120853 zig_unreachable();
2105220854 }
2105320855 }
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) {
21056 lazy_fn_type->is_generic = true;
21057 return result;
20859 if (instruction->param_types[fn_type_id.next_param_index] == nullptr) {
20860 param_info->type = nullptr;
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;
2105820890 }
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;
2106720892 }
2106820893
2106920894 if (instruction->align_value != nullptr) {
21070 lazy_fn_type->align_val = ir_resolve_const(ira, instruction->align_value->child, LazyOk);
21071 if (lazy_fn_type->align_val == nullptr)
20895 if (!ir_resolve_align(ira, instruction->align_value->child, &fn_type_id.alignment))
2107220896 return ira->codegen->invalid_instruction;
2107320897 }
2107420898
21075 lazy_fn_type->return_type = ir_resolve_const(ira, instruction->return_type->child, LazyOk);
21076 if (lazy_fn_type->return_type == nullptr)
20899 IrInstruction *return_type_value = instruction->return_type->child;
20900 fn_type_id.return_type = ir_resolve_type(ira, return_type_value);
20901 if (type_is_invalid(fn_type_id.return_type))
2107720902 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) {
2108020910 if (instruction->async_allocator_type_value == nullptr) {
2108120911 ir_add_error(ira, &instruction->base,
2108220912 buf_sprintf("async fn proto missing allocator type"));
2108320913 return ira->codegen->invalid_instruction;
2108420914 }
21085 lazy_fn_type->async_allocator_type = ir_resolve_const(ira, instruction->async_allocator_type_value->child, LazyOk);
21086 if (lazy_fn_type->async_allocator_type == nullptr)
20915 IrInstruction *async_allocator_type_value = instruction->async_allocator_type_value->child;
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))
2108720918 return ira->codegen->invalid_instruction;
2108820919 }
2108920920
21090 return result;
20921 return ir_const_type(ira, &instruction->base, get_fn_type(ira->codegen, &fn_type_id));
2109120922}
2109220923
2109320924static 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
2175621587 val->type->data.vector.len);
2175721588 case ZigTypeIdEnum:
2175821589 switch (val->type->data.enumeration.layout) {
21759 case ContainerLayoutAuto: {
21760 opt_ir_add_error_node(ira, codegen, source_node,
21761 buf_sprintf("compiler bug: TODO: implement enum byte reinterpretation"));
21762 return ErrorSemanticAnalyzeFail;
21763 }
21590 case ContainerLayoutAuto:
21591 zig_panic("TODO buf_read_value_bytes enum auto");
2176421592 case ContainerLayoutPacked:
2176521593 zig_panic("TODO buf_read_value_bytes enum packed");
2176621594 case ContainerLayoutExtern: {
......@@ -22056,7 +21884,7 @@ static IrInstruction *ir_analyze_instruction_decl_ref(IrAnalyze *ira,
2205621884 Tld *tld = instruction->tld;
2205721885 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);
2206021888 if (tld->resolution == TldResolutionInvalid)
2206121889 return ira->codegen->invalid_instruction;
2206221890
src/ir.hpp+1-2
......@@ -16,8 +16,7 @@ bool ir_gen_fn(CodeGen *g, ZigFn *fn_entry);
1616ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
1717 ZigType *expected_type, size_t *backward_branch_count, size_t backward_branch_quota,
1818 ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,
19 IrExecutable *parent_exec, AstNode *expected_type_source_node, bool allow_lazy);
20Error ir_resolve_lazy(CodeGen *codegen, AstNode *source_node, ConstExprValue *val);
19 IrExecutable *parent_exec, AstNode *expected_type_source_node);
2120
2221ZigType *ir_analyze(CodeGen *g, IrExecutable *old_executable, IrExecutable *new_executable,
2322 ZigType *expected_type, AstNode *expected_type_source_node);