authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-10-09 13:34:49-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-10-09 13:35:19-04:00
log2286003666e8216d809b30e164963a52cdb2d4f8
treecf27578b3cecf3b0570aa0766e3fa0ee5af2e167
parent8a547d9e811ab3d78b88b2a802338a0550b38902
signaturelock-open Commit is signed but in an unrecognized format.

improve names of error sets when using merge error sets operator


4 files changed, 111 insertions(+), 26 deletions(-)

src/all_types.hpp+11-1
......@@ -398,6 +398,7 @@ struct LazyValueErrUnionType {
398398 IrAnalyze *ira;
399399 IrInstruction *err_set_type;
400400 IrInstruction *payload_type;
401 Buf *type_name;
401402};
402403
403404struct ConstExprValue {
......@@ -2407,6 +2408,7 @@ enum IrInstructionId {
24072408 IrInstructionIdPhi,
24082409 IrInstructionIdUnOp,
24092410 IrInstructionIdBinOp,
2411 IrInstructionIdMergeErrSets,
24102412 IrInstructionIdLoadPtr,
24112413 IrInstructionIdLoadPtrGen,
24122414 IrInstructionIdStorePtr,
......@@ -2713,7 +2715,6 @@ enum IrBinOp {
27132715 IrBinOpRemMod,
27142716 IrBinOpArrayCat,
27152717 IrBinOpArrayMult,
2716 IrBinOpMergeErrorSets,
27172718};
27182719
27192720struct IrInstructionBinOp {
......@@ -2725,6 +2726,14 @@ struct IrInstructionBinOp {
27252726 bool safety_check_on;
27262727};
27272728
2729struct IrInstructionMergeErrSets {
2730 IrInstruction base;
2731
2732 IrInstruction *op1;
2733 IrInstruction *op2;
2734 Buf *type_name;
2735};
2736
27282737struct IrInstructionLoadPtr {
27292738 IrInstruction base;
27302739
......@@ -3633,6 +3642,7 @@ struct IrInstructionErrorUnion {
36333642
36343643 IrInstruction *err_set;
36353644 IrInstruction *payload;
3645 Buf *type_name;
36363646};
36373647
36383648struct IrInstructionAtomicRmw {
src/codegen.cpp+1-1
......@@ -2776,7 +2776,6 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
27762776 case IrBinOpArrayCat:
27772777 case IrBinOpArrayMult:
27782778 case IrBinOpRemUnspecified:
2779 case IrBinOpMergeErrorSets:
27802779 zig_unreachable();
27812780 case IrBinOpBoolOr:
27822781 return LLVMBuildOr(g->builder, op1_value, op2_value, "");
......@@ -6040,6 +6039,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
60406039 case IrInstructionIdAllocaGen:
60416040 case IrInstructionIdAwaitSrc:
60426041 case IrInstructionIdSplatSrc:
6042 case IrInstructionIdMergeErrSets:
60436043 zig_unreachable();
60446044
60456045 case IrInstructionIdDeclVarGen:
src/ir.cpp+85-22
......@@ -198,6 +198,8 @@ static IrInstruction *ir_gen_union_init_expr(IrBuilder *irb, Scope *scope, AstNo
198198 IrInstruction *union_type, IrInstruction *field_name, AstNode *expr_node,
199199 LVal lval, ResultLoc *parent_result_loc);
200200static void ir_reset_result(ResultLoc *result_loc);
201static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char *kind_name,
202 Scope *scope, AstNode *source_node, Buf *out_bare_name);
201203
202204static ConstExprValue *const_ptr_pointee_unchecked(CodeGen *g, ConstExprValue *const_val) {
203205 assert(get_src_ptr_type(const_val->type) != nullptr);
......@@ -469,6 +471,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionBinOp *) {
469471 return IrInstructionIdBinOp;
470472}
471473
474static constexpr IrInstructionId ir_instruction_id(IrInstructionMergeErrSets *) {
475 return IrInstructionIdMergeErrSets;
476}
477
472478static constexpr IrInstructionId ir_instruction_id(IrInstructionExport *) {
473479 return IrInstructionIdExport;
474480}
......@@ -1290,6 +1296,20 @@ static IrInstruction *ir_build_bin_op(IrBuilder *irb, Scope *scope, AstNode *sou
12901296 return &bin_op_instruction->base;
12911297}
12921298
1299static IrInstruction *ir_build_merge_err_sets(IrBuilder *irb, Scope *scope, AstNode *source_node,
1300 IrInstruction *op1, IrInstruction *op2, Buf *type_name)
1301{
1302 IrInstructionMergeErrSets *merge_err_sets_instruction = ir_build_instruction<IrInstructionMergeErrSets>(irb, scope, source_node);
1303 merge_err_sets_instruction->op1 = op1;
1304 merge_err_sets_instruction->op2 = op2;
1305 merge_err_sets_instruction->type_name = type_name;
1306
1307 ir_ref_instruction(op1, irb->current_basic_block);
1308 ir_ref_instruction(op2, irb->current_basic_block);
1309
1310 return &merge_err_sets_instruction->base;
1311}
1312
12931313static IrInstruction *ir_build_var_ptr_x(IrBuilder *irb, Scope *scope, AstNode *source_node, ZigVar *var,
12941314 ScopeFnDef *crossed_fndef_scope)
12951315{
......@@ -3894,6 +3914,20 @@ static IrInstruction *ir_gen_bin_op_id(IrBuilder *irb, Scope *scope, AstNode *no
38943914 return ir_build_bin_op(irb, scope, node, op_id, op1, op2, true);
38953915}
38963916
3917static IrInstruction *ir_gen_merge_err_sets(IrBuilder *irb, Scope *scope, AstNode *node) {
3918 IrInstruction *op1 = ir_gen_node(irb, node->data.bin_op_expr.op1, scope);
3919 IrInstruction *op2 = ir_gen_node(irb, node->data.bin_op_expr.op2, scope);
3920
3921 if (op1 == irb->codegen->invalid_instruction || op2 == irb->codegen->invalid_instruction)
3922 return irb->codegen->invalid_instruction;
3923
3924 // TODO only pass type_name when the || operator is the top level AST node in the var decl expr
3925 Buf bare_name = BUF_INIT;
3926 Buf *type_name = get_anon_type_name(irb->codegen, irb->exec, "error", scope, node, &bare_name);
3927
3928 return ir_build_merge_err_sets(irb, scope, node, op1, op2, type_name);
3929}
3930
38973931static IrInstruction *ir_gen_assign(IrBuilder *irb, Scope *scope, AstNode *node) {
38983932 IrInstruction *lvalue = ir_gen_node_extra(irb, node->data.bin_op_expr.op1, scope, LValPtr, nullptr);
38993933 if (lvalue == irb->codegen->invalid_instruction)
......@@ -3913,6 +3947,19 @@ static IrInstruction *ir_gen_assign(IrBuilder *irb, Scope *scope, AstNode *node)
39133947 return ir_build_const_void(irb, scope, node);
39143948}
39153949
3950static IrInstruction *ir_gen_assign_merge_err_sets(IrBuilder *irb, Scope *scope, AstNode *node) {
3951 IrInstruction *lvalue = ir_gen_node_extra(irb, node->data.bin_op_expr.op1, scope, LValPtr, nullptr);
3952 if (lvalue == irb->codegen->invalid_instruction)
3953 return lvalue;
3954 IrInstruction *op1 = ir_build_load_ptr(irb, scope, node->data.bin_op_expr.op1, lvalue);
3955 IrInstruction *op2 = ir_gen_node(irb, node->data.bin_op_expr.op2, scope);
3956 if (op2 == irb->codegen->invalid_instruction)
3957 return op2;
3958 IrInstruction *result = ir_build_merge_err_sets(irb, scope, node, op1, op2, nullptr);
3959 ir_build_store_ptr(irb, scope, node, lvalue, result);
3960 return ir_build_const_void(irb, scope, node);
3961}
3962
39163963static IrInstruction *ir_gen_assign_op(IrBuilder *irb, Scope *scope, AstNode *node, IrBinOp op_id) {
39173964 IrInstruction *lvalue = ir_gen_node_extra(irb, node->data.bin_op_expr.op1, scope, LValPtr, nullptr);
39183965 if (lvalue == irb->codegen->invalid_instruction)
......@@ -4153,7 +4200,7 @@ static IrInstruction *ir_gen_bin_op(IrBuilder *irb, Scope *scope, AstNode *node,
41534200 case BinOpTypeAssignBitOr:
41544201 return ir_lval_wrap(irb, scope, ir_gen_assign_op(irb, scope, node, IrBinOpBinOr), lval, result_loc);
41554202 case BinOpTypeAssignMergeErrorSets:
4156 return ir_lval_wrap(irb, scope, ir_gen_assign_op(irb, scope, node, IrBinOpMergeErrorSets), lval, result_loc);
4203 return ir_lval_wrap(irb, scope, ir_gen_assign_merge_err_sets(irb, scope, node), lval, result_loc);
41574204 case BinOpTypeBoolOr:
41584205 return ir_lval_wrap(irb, scope, ir_gen_bool_or(irb, scope, node), lval, result_loc);
41594206 case BinOpTypeBoolAnd:
......@@ -4201,7 +4248,7 @@ static IrInstruction *ir_gen_bin_op(IrBuilder *irb, Scope *scope, AstNode *node,
42014248 case BinOpTypeArrayMult:
42024249 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpArrayMult), lval, result_loc);
42034250 case BinOpTypeMergeErrorSets:
4204 return ir_lval_wrap(irb, scope, ir_gen_bin_op_id(irb, scope, node, IrBinOpMergeErrorSets), lval, result_loc);
4251 return ir_lval_wrap(irb, scope, ir_gen_merge_err_sets(irb, scope, node), lval, result_loc);
42054252 case BinOpTypeUnwrapOptional:
42064253 return ir_gen_orelse(irb, scope, node, lval, result_loc);
42074254 case BinOpTypeErrorUnion:
......@@ -7859,7 +7906,9 @@ static IrInstruction *ir_gen_container_decl(IrBuilder *irb, Scope *parent_scope,
78597906}
78607907
78617908// errors should be populated with set1's values
7862static ZigType *get_error_set_union(CodeGen *g, ErrorTableEntry **errors, ZigType *set1, ZigType *set2) {
7909static ZigType *get_error_set_union(CodeGen *g, ErrorTableEntry **errors, ZigType *set1, ZigType *set2,
7910 Buf *type_name)
7911{
78637912 assert(set1->id == ZigTypeIdErrorSet);
78647913 assert(set2->id == ZigTypeIdErrorSet);
78657914
......@@ -7867,8 +7916,12 @@ static ZigType *get_error_set_union(CodeGen *g, ErrorTableEntry **errors, ZigTyp
78677916 err_set_type->size_in_bits = g->builtin_types.entry_global_error_set->size_in_bits;
78687917 err_set_type->abi_align = g->builtin_types.entry_global_error_set->abi_align;
78697918 err_set_type->abi_size = g->builtin_types.entry_global_error_set->abi_size;
7870 buf_resize(&err_set_type->name, 0);
7871 buf_appendf(&err_set_type->name, "error{");
7919 if (type_name == nullptr) {
7920 buf_resize(&err_set_type->name, 0);
7921 buf_appendf(&err_set_type->name, "error{");
7922 } else {
7923 buf_init_from_buf(&err_set_type->name, type_name);
7924 }
78727925
78737926 for (uint32_t i = 0, count = set1->data.error_set.err_count; i < count; i += 1) {
78747927 assert(errors[set1->data.error_set.errors[i]->value] == set1->data.error_set.errors[i]);
......@@ -7885,21 +7938,27 @@ static ZigType *get_error_set_union(CodeGen *g, ErrorTableEntry **errors, ZigTyp
78857938 err_set_type->data.error_set.err_count = count;
78867939 err_set_type->data.error_set.errors = allocate<ErrorTableEntry *>(count);
78877940
7941 bool need_comma = false;
78887942 for (uint32_t i = 0; i < set1->data.error_set.err_count; i += 1) {
78897943 ErrorTableEntry *error_entry = set1->data.error_set.errors[i];
7890 buf_appendf(&err_set_type->name, "%s,", buf_ptr(&error_entry->name));
7944 if (type_name == nullptr) {
7945 const char *comma = need_comma ? "," : "";
7946 need_comma = true;
7947 buf_appendf(&err_set_type->name, "%s%s", comma, buf_ptr(&error_entry->name));
7948 }
78917949 err_set_type->data.error_set.errors[i] = error_entry;
78927950 }
78937951
78947952 uint32_t index = set1->data.error_set.err_count;
7895 bool need_comma = false;
78967953 for (uint32_t i = 0; i < set2->data.error_set.err_count; i += 1) {
78977954 ErrorTableEntry *error_entry = set2->data.error_set.errors[i];
78987955 if (errors[error_entry->value] == nullptr) {
78997956 errors[error_entry->value] = error_entry;
7900 const char *comma = need_comma ? "," : "";
7901 need_comma = true;
7902 buf_appendf(&err_set_type->name, "%s%s", comma, buf_ptr(&error_entry->name));
7957 if (type_name == nullptr) {
7958 const char *comma = need_comma ? "," : "";
7959 need_comma = true;
7960 buf_appendf(&err_set_type->name, "%s%s", comma, buf_ptr(&error_entry->name));
7961 }
79037962 err_set_type->data.error_set.errors[index] = error_entry;
79047963 index += 1;
79057964 }
......@@ -7907,7 +7966,9 @@ static ZigType *get_error_set_union(CodeGen *g, ErrorTableEntry **errors, ZigTyp
79077966 assert(index == count);
79087967 assert(count != 0);
79097968
7910 buf_appendf(&err_set_type->name, "}");
7969 if (type_name == nullptr) {
7970 buf_appendf(&err_set_type->name, "}");
7971 }
79117972
79127973 return err_set_type;
79137974
......@@ -9967,7 +10028,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
996710028 }
996810029
996910030 // neither of them are supersets. so we invent a new error set type that is a union of both of them
9970 err_set_type = get_error_set_union(ira->codegen, errors, cur_type, err_set_type);
10031 err_set_type = get_error_set_union(ira->codegen, errors, cur_type, err_set_type, nullptr);
997110032 assert(errors != nullptr);
997210033 continue;
997310034 } else if (cur_type->id == ZigTypeIdErrorUnion) {
......@@ -10018,7 +10079,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
1001810079 }
1001910080
1002010081 // not a subset. invent new error set type, union of both of them
10021 err_set_type = get_error_set_union(ira->codegen, errors, cur_err_set_type, err_set_type);
10082 err_set_type = get_error_set_union(ira->codegen, errors, cur_err_set_type, err_set_type, nullptr);
1002210083 prev_inst = cur_inst;
1002310084 assert(errors != nullptr);
1002410085 continue;
......@@ -10074,7 +10135,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
1007410135 continue;
1007510136 }
1007610137 // not a subset. invent new error set type, union of both of them
10077 err_set_type = get_error_set_union(ira->codegen, errors, err_set_type, cur_type);
10138 err_set_type = get_error_set_union(ira->codegen, errors, err_set_type, cur_type, nullptr);
1007810139 assert(errors != nullptr);
1007910140 continue;
1008010141 }
......@@ -10160,7 +10221,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
1016010221 continue;
1016110222 }
1016210223
10163 err_set_type = get_error_set_union(ira->codegen, errors, cur_err_set_type, prev_err_set_type);
10224 err_set_type = get_error_set_union(ira->codegen, errors, cur_err_set_type, prev_err_set_type, nullptr);
1016410225 continue;
1016510226 }
1016610227 }
......@@ -10286,7 +10347,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
1028610347
1028710348 update_errors_helper(ira->codegen, &errors, &errors_count);
1028810349
10289 err_set_type = get_error_set_union(ira->codegen, errors, err_set_type, cur_err_set_type);
10350 err_set_type = get_error_set_union(ira->codegen, errors, err_set_type, cur_err_set_type, nullptr);
1029010351 }
1029110352 prev_inst = cur_inst;
1029210353 continue;
......@@ -13795,7 +13856,6 @@ static ErrorMsg *ir_eval_math_op_scalar(IrAnalyze *ira, IrInstruction *source_in
1379513856 case IrBinOpArrayCat:
1379613857 case IrBinOpArrayMult:
1379713858 case IrBinOpRemUnspecified:
13798 case IrBinOpMergeErrorSets:
1379913859 zig_unreachable();
1380013860 case IrBinOpBinOr:
1380113861 assert(is_int);
......@@ -14102,7 +14162,6 @@ static bool ok_float_op(IrBinOp op) {
1410214162 case IrBinOpRemUnspecified:
1410314163 case IrBinOpArrayCat:
1410414164 case IrBinOpArrayMult:
14105 case IrBinOpMergeErrorSets:
1410614165 return false;
1410714166 }
1410814167 zig_unreachable();
......@@ -14603,7 +14662,9 @@ static IrInstruction *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp *
1460314662 return result;
1460414663}
1460514664
14606static IrInstruction *ir_analyze_merge_error_sets(IrAnalyze *ira, IrInstructionBinOp *instruction) {
14665static IrInstruction *ir_analyze_instruction_merge_err_sets(IrAnalyze *ira,
14666 IrInstructionMergeErrSets *instruction)
14667{
1460714668 ZigType *op1_type = ir_resolve_error_set_type(ira, &instruction->base, instruction->op1->child);
1460814669 if (type_is_invalid(op1_type))
1460914670 return ira->codegen->invalid_instruction;
......@@ -14632,12 +14693,13 @@ static IrInstruction *ir_analyze_merge_error_sets(IrAnalyze *ira, IrInstructionB
1463214693 assert(errors[error_entry->value] == nullptr);
1463314694 errors[error_entry->value] = error_entry;
1463414695 }
14635 ZigType *result_type = get_error_set_union(ira->codegen, errors, op1_type, op2_type);
14696 ZigType *result_type = get_error_set_union(ira->codegen, errors, op1_type, op2_type, instruction->type_name);
1463614697 free(errors);
1463714698
1463814699 return ir_const_type(ira, &instruction->base, result_type);
1463914700}
1464014701
14702
1464114703static IrInstruction *ir_analyze_instruction_bin_op(IrAnalyze *ira, IrInstructionBinOp *bin_op_instruction) {
1464214704 IrBinOp op_id = bin_op_instruction->op_id;
1464314705 switch (op_id) {
......@@ -14679,8 +14741,6 @@ static IrInstruction *ir_analyze_instruction_bin_op(IrAnalyze *ira, IrInstructio
1467914741 return ir_analyze_array_cat(ira, bin_op_instruction);
1468014742 case IrBinOpArrayMult:
1468114743 return ir_analyze_array_mult(ira, bin_op_instruction);
14682 case IrBinOpMergeErrorSets:
14683 return ir_analyze_merge_error_sets(ira, bin_op_instruction);
1468414744 }
1468514745 zig_unreachable();
1468614746}
......@@ -25945,6 +26005,8 @@ static IrInstruction *ir_analyze_instruction_base(IrAnalyze *ira, IrInstruction
2594526005 return ir_analyze_instruction_un_op(ira, (IrInstructionUnOp *)instruction);
2594626006 case IrInstructionIdBinOp:
2594726007 return ir_analyze_instruction_bin_op(ira, (IrInstructionBinOp *)instruction);
26008 case IrInstructionIdMergeErrSets:
26009 return ir_analyze_instruction_merge_err_sets(ira, (IrInstructionMergeErrSets *)instruction);
2594826010 case IrInstructionIdDeclVarSrc:
2594926011 return ir_analyze_instruction_decl_var(ira, (IrInstructionDeclVarSrc *)instruction);
2595026012 case IrInstructionIdLoadPtr:
......@@ -26370,6 +26432,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2637026432 case IrInstructionIdPhi:
2637126433 case IrInstructionIdUnOp:
2637226434 case IrInstructionIdBinOp:
26435 case IrInstructionIdMergeErrSets:
2637326436 case IrInstructionIdLoadPtr:
2637426437 case IrInstructionIdConst:
2637526438 case IrInstructionIdCast:
src/ir_print.cpp+14-2
......@@ -70,6 +70,8 @@ static const char* ir_instruction_type_str(IrInstruction* instruction) {
7070 return "UnOp";
7171 case IrInstructionIdBinOp:
7272 return "BinOp";
73 case IrInstructionIdMergeErrSets:
74 return "MergeErrSets";
7375 case IrInstructionIdLoadPtr:
7476 return "LoadPtr";
7577 case IrInstructionIdLoadPtrGen:
......@@ -497,8 +499,6 @@ static const char *ir_bin_op_id_str(IrBinOp op_id) {
497499 return "++";
498500 case IrBinOpArrayMult:
499501 return "**";
500 case IrBinOpMergeErrorSets:
501 return "||";
502502 }
503503 zig_unreachable();
504504}
......@@ -535,6 +535,15 @@ static void ir_print_bin_op(IrPrint *irp, IrInstructionBinOp *bin_op_instruction
535535 }
536536}
537537
538static void ir_print_merge_err_sets(IrPrint *irp, IrInstructionMergeErrSets *instruction) {
539 ir_print_other_instruction(irp, instruction->op1);
540 fprintf(irp->f, " || ");
541 ir_print_other_instruction(irp, instruction->op2);
542 if (instruction->type_name != nullptr) {
543 fprintf(irp->f, " // name=%s", buf_ptr(instruction->type_name));
544 }
545}
546
538547static void ir_print_decl_var_src(IrPrint *irp, IrInstructionDeclVarSrc *decl_var_instruction) {
539548 const char *var_or_const = decl_var_instruction->var->gen_is_const ? "const" : "var";
540549 const char *name = decl_var_instruction->var->name;
......@@ -1974,6 +1983,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction, bool
19741983 case IrInstructionIdBinOp:
19751984 ir_print_bin_op(irp, (IrInstructionBinOp *)instruction);
19761985 break;
1986 case IrInstructionIdMergeErrSets:
1987 ir_print_merge_err_sets(irp, (IrInstructionMergeErrSets *)instruction);
1988 break;
19771989 case IrInstructionIdDeclVarSrc:
19781990 ir_print_decl_var_src(irp, (IrInstructionDeclVarSrc *)instruction);
19791991 break;