authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-10 17:11:35-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-10 17:11:35-05:00
log443e14afbd0615bd4902e53a31f70550a8d4497e
tree1c166af90a15639f54dd9931972c32918a25b251
parent0ab953acb2b7192c5907a1c9d9ed9cf850530af2

IR: fix errorName builtin


3 files changed, 43 insertions(+), 11 deletions(-)

src/all_types.hpp+3
......@@ -1232,6 +1232,9 @@ struct ErrorTableEntry {
12321232 Buf name;
12331233 uint32_t value;
12341234 AstNode *decl_node;
1235 // If we generate a constant error name value for this error, we memoize it here.
1236 // The type of this is array
1237 ConstExprValue *cached_error_name_val;
12351238};
12361239
12371240struct LabelTableEntry {
src/ir.cpp+30-11
......@@ -512,12 +512,7 @@ static IrInstruction *ir_build_const_bound_fn(IrBuilder *irb, Scope *scope, AstN
512512 return &const_instruction->base;
513513}
514514
515static IrInstruction *ir_create_const_str_lit(IrBuilder *irb, Scope *scope, AstNode *source_node, Buf *str) {
516 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(irb->exec, scope, source_node);
517 TypeTableEntry *u8_type = irb->codegen->builtin_types.entry_u8;
518 TypeTableEntry *type_entry = get_array_type(irb->codegen, u8_type, buf_len(str));
519 const_instruction->base.type_entry = type_entry;
520 ConstExprValue *const_val = &const_instruction->base.static_value;
515static void init_const_str_lit(ConstExprValue *const_val, Buf *str) {
521516 const_val->special = ConstValSpecialStatic;
522517 const_val->data.x_array.elements = allocate<ConstExprValue>(buf_len(str));
523518 const_val->data.x_array.size = buf_len(str);
......@@ -527,6 +522,15 @@ static IrInstruction *ir_create_const_str_lit(IrBuilder *irb, Scope *scope, AstN
527522 this_char->special = ConstValSpecialStatic;
528523 bignum_init_unsigned(&this_char->data.x_bignum, buf_ptr(str)[i]);
529524 }
525}
526
527static IrInstruction *ir_create_const_str_lit(IrBuilder *irb, Scope *scope, AstNode *source_node, Buf *str) {
528 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(irb->exec, scope, source_node);
529 TypeTableEntry *u8_type = irb->codegen->builtin_types.entry_u8;
530 TypeTableEntry *type_entry = get_array_type(irb->codegen, u8_type, buf_len(str));
531 const_instruction->base.type_entry = type_entry;
532 ConstExprValue *const_val = &const_instruction->base.static_value;
533 init_const_str_lit(const_val, str);
530534
531535 return &const_instruction->base;
532536}
......@@ -6877,11 +6881,26 @@ static TypeTableEntry *ir_analyze_instruction_err_name(IrAnalyze *ira, IrInstruc
68776881 TypeTableEntry *str_type = get_slice_type(ira->codegen, ira->codegen->builtin_types.entry_u8, true);
68786882 if (casted_value->static_value.special == ConstValSpecialStatic) {
68796883 ErrorTableEntry *err = casted_value->static_value.data.x_pure_err;
6880 IrInstruction *new_instruction = ir_create_const_str_lit(&ira->new_irb, instruction->base.scope,
6881 instruction->base.source_node, &err->name);
6882 ir_link_new_instruction(new_instruction, &instruction->base);
6883 new_instruction->static_value.special = ConstValSpecialStatic;
6884 new_instruction->static_value.depends_on_compile_var = casted_value->static_value.depends_on_compile_var;
6884 if (!err->cached_error_name_val) {
6885 err->cached_error_name_val = allocate<ConstExprValue>(1);
6886 err->cached_error_name_val->special = ConstValSpecialStatic;
6887 err->cached_error_name_val->data.x_struct.fields = allocate<ConstExprValue>(2);
6888
6889 ConstExprValue *array_val = allocate<ConstExprValue>(1);
6890 init_const_str_lit(array_val, &err->name);
6891
6892 ConstExprValue *ptr_val = &err->cached_error_name_val->data.x_struct.fields[slice_ptr_index];
6893 ptr_val->special = ConstValSpecialStatic;
6894 ptr_val->data.x_ptr.base_ptr = array_val;
6895 ptr_val->data.x_ptr.index = 0;
6896
6897 ConstExprValue *len_val = &err->cached_error_name_val->data.x_struct.fields[slice_len_index];
6898 len_val->special = ConstValSpecialStatic;
6899 bignum_init_unsigned(&len_val->data.x_bignum, buf_len(&err->name));
6900 }
6901 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base,
6902 casted_value->static_value.depends_on_compile_var);
6903 *out_val = *err->cached_error_name_val;
68856904 return str_type;
68866905 }
68876906
test/self_hosted2.zig+10
......@@ -258,6 +258,15 @@ pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool {
258258 return true;
259259}
260260
261error ItBroke;
262fn gimmeItBroke() -> []const u8 {
263 @errorName(error.ItBroke)
264}
265
266fn testErrorName() {
267 assert(memeql(@errorName(error.ItBroke), "ItBroke"));
268}
269
261270fn assert(ok: bool) {
262271 if (!ok)
263272 @unreachable();
......@@ -284,6 +293,7 @@ fn runAllTests() {
284293 testInlineVarsAgain();
285294 testMinValueAndMaxValue();
286295 testReturnStringFromFunction();
296 testErrorName();
287297}
288298
289299export nakedcc fn _start() -> unreachable {