diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2016-07-09 15:21:50 -0700 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2016-07-09 15:22:04 -0700 |
| commit | 49a4b1b9309de06f7a359836788dfff447003f1b (patch) | |
| tree | 4ee4ce61cf93e17f0b6cdcbfd35a0935995897ee /src | |
| parent | a5251a1c102334a5dcb07e4fcc2b676be55fe2be (diff) | |
| download | zig-49a4b1b9309de06f7a359836788dfff447003f1b.tar.gz zig-49a4b1b9309de06f7a359836788dfff447003f1b.zip | |
ability to cast explicitly from int to enum
This commit also fixes a bug where pure functions are marked with
the read-only attribute in debug mode. This resulted in incorrect
codegen because calls to read-only functions with unused values
were not generated.
For example, a call to assert() would not be generated if assert
is marked with read-only. Which it *is* marked with in release
mode.
Diffstat (limited to 'src')
| -rw-r--r-- | src/all_types.hpp | 1 | ||||
| -rw-r--r-- | src/analyze.cpp | 8 | ||||
| -rw-r--r-- | src/codegen.cpp | 4 | ||||
| -rw-r--r-- | src/eval.cpp | 10 |
4 files changed, 22 insertions, 1 deletions
diff --git a/src/all_types.hpp b/src/all_types.hpp index 7e0655eacf..329bbb4249 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -383,6 +383,7 @@ enum CastOp { CastOpFloatToInt, CastOpBoolToInt, CastOpResizeSlice, + CastOpIntToEnum, }; struct AstNodeFnCallExpr { diff --git a/src/analyze.cpp b/src/analyze.cpp index 26c85ea68f..8e3ce507f8 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -4428,6 +4428,14 @@ static TypeTableEntry *analyze_cast_expr(CodeGen *g, ImportTableEntry *import, B } } + // explicit cast from integer to enum type with no payload + if (actual_type->id == TypeTableEntryIdInt && + wanted_type->id == TypeTableEntryIdEnum && + wanted_type->data.enumeration.gen_field_count == 0) + { + return resolve_cast(g, context, node, expr_node, wanted_type, CastOpIntToEnum, false); + } + add_node_error(g, node, buf_sprintf("invalid cast from type '%s' to '%s'", buf_ptr(&actual_type->name), diff --git a/src/codegen.cpp b/src/codegen.cpp index b25a3b6869..43fc41bed9 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -1032,6 +1032,8 @@ static LLVMValueRef gen_cast_expr(CodeGen *g, AstNode *node) { set_debug_source_node(g, node); return LLVMBuildZExt(g->builder, expr_val, wanted_type->type_ref, ""); + case CastOpIntToEnum: + return gen_widen_or_shorten(g, node, actual_type, wanted_type->data.enumeration.tag_type, expr_val); } zig_unreachable(); } @@ -3913,7 +3915,7 @@ static void do_code_gen(CodeGen *g) { LLVMZigAddNonNullAttr(fn_table_entry->fn_value, 1); is_sret = true; } - if (fn_table_entry->is_pure && !is_sret) { + if (fn_table_entry->is_pure && !is_sret && g->is_release_build) { LLVMAddFunctionAttr(fn_table_entry->fn_value, LLVMReadOnlyAttribute); } diff --git a/src/eval.cpp b/src/eval.cpp index ca53388386..f7ef68467a 100644 --- a/src/eval.cpp +++ b/src/eval.cpp @@ -657,6 +657,16 @@ void eval_const_expr_implicit_cast(CastOp cast_op, bignum_init_unsigned(&const_val->data.x_bignum, other_val->data.x_bool ? 1 : 0); const_val->ok = true; break; + case CastOpIntToEnum: + { + uint64_t value = other_val->data.x_bignum.data.x_uint; + assert(new_type->id == TypeTableEntryIdEnum); + assert(value < new_type->data.enumeration.field_count); + const_val->data.x_enum.tag = value; + const_val->data.x_enum.payload = NULL; + const_val->ok = true; + break; + } } } |
