authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-05-30 10:34:20+02:00
committergravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-05-30 10:34:20+02:00
log1b3aaacba260e4c8d89ac98ab856ff9b3c77dac4
tree2bc1d01d4daa8db2451fd91834b241a0018d6c9d
parent2b3af4ef6b79b8fe178656b069f837eff82ae8c3

Removed copy-pasted resolve_inferred_error_set

both ir.cpp and analyze.cpp have a function resolve_inferred_error_set, which is a nearly exact copy-paste. This commit removes the one in ir.cpp and exposes then one in analyze.cpp. This also allows us to make analyze_fn_body local to analyze.cpp, as it is not used anywhere in ir.cpp after this change

3 files changed, 25 insertions(+), 46 deletions(-)

src/analyze.cpp+4-3
...@@ -25,6 +25,7 @@ static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type);...@@ -25,6 +25,7 @@ static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type);
25static void resolve_struct_zero_bits(CodeGen *g, TypeTableEntry *struct_type);25static void resolve_struct_zero_bits(CodeGen *g, TypeTableEntry *struct_type);
26static void resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type);26static void resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type);
27static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type);27static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type);
28static void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry);
2829
29ErrorMsg *add_node_error(CodeGen *g, AstNode *node, Buf *msg) {30ErrorMsg *add_node_error(CodeGen *g, AstNode *node, Buf *msg) {
30 if (node->owner->c_import_node != nullptr) {31 if (node->owner->c_import_node != nullptr) {
...@@ -3880,7 +3881,7 @@ static void define_local_param_variables(CodeGen *g, FnTableEntry *fn_table_entr...@@ -3880,7 +3881,7 @@ static void define_local_param_variables(CodeGen *g, FnTableEntry *fn_table_entr
3880 }3881 }
3881}3882}
38823883
3883static bool analyze_resolve_inferred_error_set(CodeGen *g, TypeTableEntry *err_set_type, AstNode *source_node) {3884bool resolve_inferred_error_set(CodeGen *g, TypeTableEntry *err_set_type, AstNode *source_node) {
3884 FnTableEntry *infer_fn = err_set_type->data.error_set.infer_fn;3885 FnTableEntry *infer_fn = err_set_type->data.error_set.infer_fn;
3885 if (infer_fn != nullptr) {3886 if (infer_fn != nullptr) {
3886 if (infer_fn->anal_state == FnAnalStateInvalid) {3887 if (infer_fn->anal_state == FnAnalStateInvalid) {
...@@ -3932,7 +3933,7 @@ void analyze_fn_ir(CodeGen *g, FnTableEntry *fn_table_entry, AstNode *return_typ...@@ -3932,7 +3933,7 @@ void analyze_fn_ir(CodeGen *g, FnTableEntry *fn_table_entry, AstNode *return_typ
3932 }3933 }
39333934
3934 if (inferred_err_set_type->data.error_set.infer_fn != nullptr) {3935 if (inferred_err_set_type->data.error_set.infer_fn != nullptr) {
3935 if (!analyze_resolve_inferred_error_set(g, inferred_err_set_type, return_type_node)) {3936 if (!resolve_inferred_error_set(g, inferred_err_set_type, return_type_node)) {
3936 fn_table_entry->anal_state = FnAnalStateInvalid;3937 fn_table_entry->anal_state = FnAnalStateInvalid;
3937 return;3938 return;
3938 }3939 }
...@@ -3962,7 +3963,7 @@ void analyze_fn_ir(CodeGen *g, FnTableEntry *fn_table_entry, AstNode *return_typ...@@ -3962,7 +3963,7 @@ void analyze_fn_ir(CodeGen *g, FnTableEntry *fn_table_entry, AstNode *return_typ
3962 fn_table_entry->anal_state = FnAnalStateComplete;3963 fn_table_entry->anal_state = FnAnalStateComplete;
3963}3964}
39643965
3965void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry) {3966static void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry) {
3966 assert(fn_table_entry->anal_state != FnAnalStateProbing);3967 assert(fn_table_entry->anal_state != FnAnalStateProbing);
3967 if (fn_table_entry->anal_state != FnAnalStateReady)3968 if (fn_table_entry->anal_state != FnAnalStateReady)
3968 return;3969 return;
src/analyze.hpp+1-1
...@@ -191,7 +191,7 @@ void add_fn_export(CodeGen *g, FnTableEntry *fn_table_entry, Buf *symbol_name, G...@@ -191,7 +191,7 @@ void add_fn_export(CodeGen *g, FnTableEntry *fn_table_entry, Buf *symbol_name, G
191191
192ConstExprValue *get_builtin_value(CodeGen *codegen, const char *name);192ConstExprValue *get_builtin_value(CodeGen *codegen, const char *name);
193TypeTableEntry *get_ptr_to_stack_trace_type(CodeGen *g);193TypeTableEntry *get_ptr_to_stack_trace_type(CodeGen *g);
194void analyze_fn_body(CodeGen *g, FnTableEntry *fn_table_entry);194bool resolve_inferred_error_set(CodeGen *g, TypeTableEntry *err_set_type, AstNode *source_node);
195195
196TypeTableEntry *get_auto_err_set_type(CodeGen *g, FnTableEntry *fn_entry);196TypeTableEntry *get_auto_err_set_type(CodeGen *g, FnTableEntry *fn_entry);
197197
src/ir.cpp+20-42
...@@ -7633,38 +7633,16 @@ static bool slice_is_const(TypeTableEntry *type) {...@@ -7633,38 +7633,16 @@ static bool slice_is_const(TypeTableEntry *type) {
7633 return type->data.structure.fields[slice_ptr_index].type_entry->data.pointer.is_const;7633 return type->data.structure.fields[slice_ptr_index].type_entry->data.pointer.is_const;
7634}7634}
76357635
7636static bool resolve_inferred_error_set(IrAnalyze *ira, TypeTableEntry *err_set_type, AstNode *source_node) {
7637 assert(err_set_type->id == TypeTableEntryIdErrorSet);
7638 FnTableEntry *infer_fn = err_set_type->data.error_set.infer_fn;
7639 if (infer_fn != nullptr) {
7640 if (infer_fn->anal_state == FnAnalStateInvalid) {
7641 return false;
7642 } else if (infer_fn->anal_state == FnAnalStateReady) {
7643 analyze_fn_body(ira->codegen, infer_fn);
7644 if (err_set_type->data.error_set.infer_fn != nullptr) {
7645 assert(ira->codegen->errors.length != 0);
7646 return false;
7647 }
7648 } else {
7649 ir_add_error_node(ira, source_node,
7650 buf_sprintf("cannot resolve inferred error set '%s': function '%s' not fully analyzed yet",
7651 buf_ptr(&err_set_type->name), buf_ptr(&err_set_type->data.error_set.infer_fn->symbol_name)));
7652 return false;
7653 }
7654 }
7655 return true;
7656}
7657
7658static TypeTableEntry *get_error_set_intersection(IrAnalyze *ira, TypeTableEntry *set1, TypeTableEntry *set2,7636static TypeTableEntry *get_error_set_intersection(IrAnalyze *ira, TypeTableEntry *set1, TypeTableEntry *set2,
7659 AstNode *source_node)7637 AstNode *source_node)
7660{7638{
7661 assert(set1->id == TypeTableEntryIdErrorSet);7639 assert(set1->id == TypeTableEntryIdErrorSet);
7662 assert(set2->id == TypeTableEntryIdErrorSet);7640 assert(set2->id == TypeTableEntryIdErrorSet);
76637641
7664 if (!resolve_inferred_error_set(ira, set1, source_node)) {7642 if (!resolve_inferred_error_set(ira->codegen, set1, source_node)) {
7665 return ira->codegen->builtin_types.entry_invalid;7643 return ira->codegen->builtin_types.entry_invalid;
7666 }7644 }
7667 if (!resolve_inferred_error_set(ira, set2, source_node)) {7645 if (!resolve_inferred_error_set(ira->codegen, set2, source_node)) {
7668 return ira->codegen->builtin_types.entry_invalid;7646 return ira->codegen->builtin_types.entry_invalid;
7669 }7647 }
7670 if (type_is_global_error_set(set1)) {7648 if (type_is_global_error_set(set1)) {
...@@ -7803,7 +7781,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, TypeTableEntry...@@ -7803,7 +7781,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, TypeTableEntry
7803 return result;7781 return result;
7804 }7782 }
78057783
7806 if (!resolve_inferred_error_set(ira, contained_set, source_node)) {7784 if (!resolve_inferred_error_set(ira->codegen, contained_set, source_node)) {
7807 result.id = ConstCastResultIdUnresolvedInferredErrSet;7785 result.id = ConstCastResultIdUnresolvedInferredErrSet;
7808 return result;7786 return result;
7809 }7787 }
...@@ -8192,7 +8170,7 @@ static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_nod...@@ -8192,7 +8170,7 @@ static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_nod
8192 err_set_type = ira->codegen->builtin_types.entry_global_error_set;8170 err_set_type = ira->codegen->builtin_types.entry_global_error_set;
8193 } else {8171 } else {
8194 err_set_type = prev_inst->value.type;8172 err_set_type = prev_inst->value.type;
8195 if (!resolve_inferred_error_set(ira, err_set_type, prev_inst->source_node)) {8173 if (!resolve_inferred_error_set(ira->codegen, err_set_type, prev_inst->source_node)) {
8196 return ira->codegen->builtin_types.entry_invalid;8174 return ira->codegen->builtin_types.entry_invalid;
8197 }8175 }
8198 update_errors_helper(ira->codegen, &errors, &errors_count);8176 update_errors_helper(ira->codegen, &errors, &errors_count);
...@@ -8231,7 +8209,7 @@ static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_nod...@@ -8231,7 +8209,7 @@ static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_nod
8231 if (type_is_global_error_set(err_set_type)) {8209 if (type_is_global_error_set(err_set_type)) {
8232 continue;8210 continue;
8233 }8211 }
8234 if (!resolve_inferred_error_set(ira, cur_type, cur_inst->source_node)) {8212 if (!resolve_inferred_error_set(ira->codegen, cur_type, cur_inst->source_node)) {
8235 return ira->codegen->builtin_types.entry_invalid;8213 return ira->codegen->builtin_types.entry_invalid;
8236 }8214 }
8237 if (type_is_global_error_set(cur_type)) {8215 if (type_is_global_error_set(cur_type)) {
...@@ -8297,7 +8275,7 @@ static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_nod...@@ -8297,7 +8275,7 @@ static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_nod
8297 continue;8275 continue;
8298 }8276 }
8299 TypeTableEntry *cur_err_set_type = cur_type->data.error_union.err_set_type;8277 TypeTableEntry *cur_err_set_type = cur_type->data.error_union.err_set_type;
8300 if (!resolve_inferred_error_set(ira, cur_err_set_type, cur_inst->source_node)) {8278 if (!resolve_inferred_error_set(ira->codegen, cur_err_set_type, cur_inst->source_node)) {
8301 return ira->codegen->builtin_types.entry_invalid;8279 return ira->codegen->builtin_types.entry_invalid;
8302 }8280 }
8303 if (type_is_global_error_set(cur_err_set_type)) {8281 if (type_is_global_error_set(cur_err_set_type)) {
...@@ -8360,7 +8338,7 @@ static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_nod...@@ -8360,7 +8338,7 @@ static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_nod
8360 if (err_set_type != nullptr && type_is_global_error_set(err_set_type)) {8338 if (err_set_type != nullptr && type_is_global_error_set(err_set_type)) {
8361 continue;8339 continue;
8362 }8340 }
8363 if (!resolve_inferred_error_set(ira, cur_type, cur_inst->source_node)) {8341 if (!resolve_inferred_error_set(ira->codegen, cur_type, cur_inst->source_node)) {
8364 return ira->codegen->builtin_types.entry_invalid;8342 return ira->codegen->builtin_types.entry_invalid;
8365 }8343 }
83668344
...@@ -8417,11 +8395,11 @@ static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_nod...@@ -8417,11 +8395,11 @@ static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_nod
8417 TypeTableEntry *prev_err_set_type = (err_set_type == nullptr) ? prev_type->data.error_union.err_set_type : err_set_type;8395 TypeTableEntry *prev_err_set_type = (err_set_type == nullptr) ? prev_type->data.error_union.err_set_type : err_set_type;
8418 TypeTableEntry *cur_err_set_type = cur_type->data.error_union.err_set_type;8396 TypeTableEntry *cur_err_set_type = cur_type->data.error_union.err_set_type;
84198397
8420 if (!resolve_inferred_error_set(ira, prev_err_set_type, cur_inst->source_node)) {8398 if (!resolve_inferred_error_set(ira->codegen, prev_err_set_type, cur_inst->source_node)) {
8421 return ira->codegen->builtin_types.entry_invalid;8399 return ira->codegen->builtin_types.entry_invalid;
8422 }8400 }
84238401
8424 if (!resolve_inferred_error_set(ira, cur_err_set_type, cur_inst->source_node)) {8402 if (!resolve_inferred_error_set(ira->codegen, cur_err_set_type, cur_inst->source_node)) {
8425 return ira->codegen->builtin_types.entry_invalid;8403 return ira->codegen->builtin_types.entry_invalid;
8426 }8404 }
84278405
...@@ -8531,7 +8509,7 @@ static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_nod...@@ -8531,7 +8509,7 @@ static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_nod
8531 {8509 {
8532 if (err_set_type != nullptr) {8510 if (err_set_type != nullptr) {
8533 TypeTableEntry *cur_err_set_type = cur_type->data.error_union.err_set_type;8511 TypeTableEntry *cur_err_set_type = cur_type->data.error_union.err_set_type;
8534 if (!resolve_inferred_error_set(ira, cur_err_set_type, cur_inst->source_node)) {8512 if (!resolve_inferred_error_set(ira->codegen, cur_err_set_type, cur_inst->source_node)) {
8535 return ira->codegen->builtin_types.entry_invalid;8513 return ira->codegen->builtin_types.entry_invalid;
8536 }8514 }
8537 if (type_is_global_error_set(cur_err_set_type) || type_is_global_error_set(err_set_type)) {8515 if (type_is_global_error_set(cur_err_set_type) || type_is_global_error_set(err_set_type)) {
...@@ -9213,7 +9191,7 @@ static IrInstruction *ir_analyze_err_set_cast(IrAnalyze *ira, IrInstruction *sou...@@ -9213,7 +9191,7 @@ static IrInstruction *ir_analyze_err_set_cast(IrAnalyze *ira, IrInstruction *sou
9213 if (!val)9191 if (!val)
9214 return ira->codegen->invalid_instruction;9192 return ira->codegen->invalid_instruction;
92159193
9216 if (!resolve_inferred_error_set(ira, wanted_type, source_instr->source_node)) {9194 if (!resolve_inferred_error_set(ira->codegen, wanted_type, source_instr->source_node)) {
9217 return ira->codegen->invalid_instruction;9195 return ira->codegen->invalid_instruction;
9218 }9196 }
9219 if (!type_is_global_error_set(wanted_type)) {9197 if (!type_is_global_error_set(wanted_type)) {
...@@ -9654,7 +9632,7 @@ static IrInstruction *ir_analyze_int_to_err(IrAnalyze *ira, IrInstruction *sourc...@@ -9654,7 +9632,7 @@ static IrInstruction *ir_analyze_int_to_err(IrAnalyze *ira, IrInstruction *sourc
9654 IrInstruction *result = ir_create_const(&ira->new_irb, source_instr->scope,9632 IrInstruction *result = ir_create_const(&ira->new_irb, source_instr->scope,
9655 source_instr->source_node, wanted_type);9633 source_instr->source_node, wanted_type);
96569634
9657 if (!resolve_inferred_error_set(ira, wanted_type, source_instr->source_node)) {9635 if (!resolve_inferred_error_set(ira->codegen, wanted_type, source_instr->source_node)) {
9658 return ira->codegen->invalid_instruction;9636 return ira->codegen->invalid_instruction;
9659 }9637 }
96609638
...@@ -9752,7 +9730,7 @@ static IrInstruction *ir_analyze_err_to_int(IrAnalyze *ira, IrInstruction *sourc...@@ -9752,7 +9730,7 @@ static IrInstruction *ir_analyze_err_to_int(IrAnalyze *ira, IrInstruction *sourc
9752 zig_unreachable();9730 zig_unreachable();
9753 }9731 }
9754 if (!type_is_global_error_set(err_set_type)) {9732 if (!type_is_global_error_set(err_set_type)) {
9755 if (!resolve_inferred_error_set(ira, err_set_type, source_instr->source_node)) {9733 if (!resolve_inferred_error_set(ira->codegen, err_set_type, source_instr->source_node)) {
9756 return ira->codegen->invalid_instruction;9734 return ira->codegen->invalid_instruction;
9757 }9735 }
9758 if (err_set_type->data.error_set.err_count == 0) {9736 if (err_set_type->data.error_set.err_count == 0) {
...@@ -10647,7 +10625,7 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp...@@ -10647,7 +10625,7 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp
10647 return ira->codegen->builtin_types.entry_invalid;10625 return ira->codegen->builtin_types.entry_invalid;
10648 }10626 }
1064910627
10650 if (!resolve_inferred_error_set(ira, intersect_type, source_node)) {10628 if (!resolve_inferred_error_set(ira->codegen, intersect_type, source_node)) {
10651 return ira->codegen->builtin_types.entry_invalid;10629 return ira->codegen->builtin_types.entry_invalid;
10652 }10630 }
1065310631
...@@ -11503,11 +11481,11 @@ static TypeTableEntry *ir_analyze_merge_error_sets(IrAnalyze *ira, IrInstruction...@@ -11503,11 +11481,11 @@ static TypeTableEntry *ir_analyze_merge_error_sets(IrAnalyze *ira, IrInstruction
11503 return ira->codegen->builtin_types.entry_type;11481 return ira->codegen->builtin_types.entry_type;
11504 }11482 }
1150511483
11506 if (!resolve_inferred_error_set(ira, op1_type, instruction->op1->other->source_node)) {11484 if (!resolve_inferred_error_set(ira->codegen, op1_type, instruction->op1->other->source_node)) {
11507 return ira->codegen->builtin_types.entry_invalid;11485 return ira->codegen->builtin_types.entry_invalid;
11508 }11486 }
1150911487
11510 if (!resolve_inferred_error_set(ira, op2_type, instruction->op2->other->source_node)) {11488 if (!resolve_inferred_error_set(ira->codegen, op2_type, instruction->op2->other->source_node)) {
11511 return ira->codegen->builtin_types.entry_invalid;11489 return ira->codegen->builtin_types.entry_invalid;
11512 }11490 }
1151311491
...@@ -13851,7 +13829,7 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru...@@ -13851,7 +13829,7 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
13851 }13829 }
13852 err_set_type = err_entry->set_with_only_this_in_it;13830 err_set_type = err_entry->set_with_only_this_in_it;
13853 } else {13831 } else {
13854 if (!resolve_inferred_error_set(ira, child_type, field_ptr_instruction->base.source_node)) {13832 if (!resolve_inferred_error_set(ira->codegen, child_type, field_ptr_instruction->base.source_node)) {
13855 return ira->codegen->builtin_types.entry_invalid;13833 return ira->codegen->builtin_types.entry_invalid;
13856 }13834 }
13857 err_entry = find_err_table_entry(child_type, field_name);13835 err_entry = find_err_table_entry(child_type, field_name);
...@@ -17559,7 +17537,7 @@ static TypeTableEntry *ir_analyze_instruction_member_count(IrAnalyze *ira, IrIns...@@ -17559,7 +17537,7 @@ static TypeTableEntry *ir_analyze_instruction_member_count(IrAnalyze *ira, IrIns
17559 } else if (container_type->id == TypeTableEntryIdUnion) {17537 } else if (container_type->id == TypeTableEntryIdUnion) {
17560 result = container_type->data.unionation.src_field_count;17538 result = container_type->data.unionation.src_field_count;
17561 } else if (container_type->id == TypeTableEntryIdErrorSet) {17539 } else if (container_type->id == TypeTableEntryIdErrorSet) {
17562 if (!resolve_inferred_error_set(ira, container_type, instruction->base.source_node)) {17540 if (!resolve_inferred_error_set(ira->codegen, container_type, instruction->base.source_node)) {
17563 return ira->codegen->builtin_types.entry_invalid;17541 return ira->codegen->builtin_types.entry_invalid;
17564 }17542 }
17565 if (type_is_global_error_set(container_type)) {17543 if (type_is_global_error_set(container_type)) {
...@@ -17863,7 +17841,7 @@ static TypeTableEntry *ir_analyze_instruction_test_err(IrAnalyze *ira, IrInstruc...@@ -17863,7 +17841,7 @@ static TypeTableEntry *ir_analyze_instruction_test_err(IrAnalyze *ira, IrInstruc
17863 }17841 }
1786417842
17865 TypeTableEntry *err_set_type = type_entry->data.error_union.err_set_type;17843 TypeTableEntry *err_set_type = type_entry->data.error_union.err_set_type;
17866 if (!resolve_inferred_error_set(ira, err_set_type, instruction->base.source_node)) {17844 if (!resolve_inferred_error_set(ira->codegen, err_set_type, instruction->base.source_node)) {
17867 return ira->codegen->builtin_types.entry_invalid;17845 return ira->codegen->builtin_types.entry_invalid;
17868 }17846 }
17869 if (!type_is_global_error_set(err_set_type) &&17847 if (!type_is_global_error_set(err_set_type) &&
...@@ -18131,7 +18109,7 @@ static TypeTableEntry *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira...@@ -18131,7 +18109,7 @@ static TypeTableEntry *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira
18131 }18109 }
18132 }18110 }
18133 } else if (switch_type->id == TypeTableEntryIdErrorSet) {18111 } else if (switch_type->id == TypeTableEntryIdErrorSet) {
18134 if (!resolve_inferred_error_set(ira, switch_type, target_value->source_node)) {18112 if (!resolve_inferred_error_set(ira->codegen, switch_type, target_value->source_node)) {
18135 return ira->codegen->builtin_types.entry_invalid;18113 return ira->codegen->builtin_types.entry_invalid;
18136 }18114 }
1813718115