diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2016-04-19 17:15:36 -0700 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2016-04-19 17:15:55 -0700 |
| commit | 4e37fb2fa244e350b4e248332848d40594eee820 (patch) | |
| tree | 02de8b32d21a4b673484d69a1f7e223b48bdd523 /src/codegen.cpp | |
| parent | 9658c05fd406a13cade56b9c6e26d96764b965cc (diff) | |
| download | zig-4e37fb2fa244e350b4e248332848d40594eee820.tar.gz zig-4e37fb2fa244e350b4e248332848d40594eee820.zip | |
implement constant initialization of enum values
see #5
Diffstat (limited to 'src/codegen.cpp')
| -rw-r--r-- | src/codegen.cpp | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/src/codegen.cpp b/src/codegen.cpp index 0f2a63d3fc..aa383b9c3b 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -2937,7 +2937,23 @@ static LLVMValueRef gen_const_val(CodeGen *g, TypeTableEntry *type_entry, ConstE assert(enum_field->value == const_val->data.x_enum.tag); LLVMValueRef union_value; if (type_has_bits(enum_field->type_entry)) { - union_value = gen_const_val(g, union_type, const_val->data.x_enum.payload); + uint64_t union_type_bytes = LLVMStoreSizeOfType(g->target_data_ref, + union_type->type_ref); + uint64_t field_type_bytes = LLVMStoreSizeOfType(g->target_data_ref, + enum_field->type_entry->type_ref); + uint64_t pad_bytes = union_type_bytes - field_type_bytes; + + LLVMValueRef correctly_typed_value = gen_const_val(g, enum_field->type_entry, + const_val->data.x_enum.payload); + if (pad_bytes == 0) { + union_value = correctly_typed_value; + } else { + LLVMValueRef fields[] = { + correctly_typed_value, + LLVMGetUndef(LLVMArrayType(LLVMInt8Type(), pad_bytes)), + }; + union_value = LLVMConstStruct(fields, 2, false); + } } else { union_value = LLVMGetUndef(union_type->type_ref); } |
