| ... | ... | @@ -4351,17 +4351,32 @@ static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutable *executab |
| 4351 | 4351 | TypeUnionField *field = instruction->field; |
| 4352 | 4352 | |
| 4353 | 4353 | if (!type_has_bits(field->type_entry)) { |
| 4354 | | if (union_type->data.unionation.gen_tag_index == SIZE_MAX) { |
| 4354 | ZigType *tag_type = union_type->data.unionation.tag_type; |
| 4355 | if (!instruction->initializing || !type_has_bits(tag_type)) |
| 4355 | 4356 | return nullptr; |
| 4357 | |
| 4358 | // The field has no bits but we still have to change the discriminant |
| 4359 | // value here |
| 4360 | LLVMValueRef union_ptr = ir_llvm_value(g, instruction->union_ptr); |
| 4361 | |
| 4362 | LLVMTypeRef tag_type_ref = get_llvm_type(g, tag_type); |
| 4363 | LLVMValueRef tag_field_ptr = nullptr; |
| 4364 | if (union_type->data.unionation.gen_field_count == 0) { |
| 4365 | assert(union_type->data.unionation.gen_tag_index == SIZE_MAX); |
| 4366 | // The whole union is collapsed into the discriminant |
| 4367 | tag_field_ptr = LLVMBuildBitCast(g->builder, union_ptr, |
| 4368 | LLVMPointerType(tag_type_ref, 0), ""); |
| 4369 | } else { |
| 4370 | assert(union_type->data.unionation.gen_tag_index != SIZE_MAX); |
| 4371 | tag_field_ptr = LLVMBuildStructGEP(g->builder, union_ptr, |
| 4372 | union_type->data.unionation.gen_tag_index, ""); |
| 4356 | 4373 | } |
| 4357 | | if (instruction->initializing) { |
| 4358 | | LLVMValueRef union_ptr = ir_llvm_value(g, instruction->union_ptr); |
| 4359 | | LLVMValueRef tag_field_ptr = LLVMBuildStructGEP(g->builder, union_ptr, |
| 4360 | | union_type->data.unionation.gen_tag_index, ""); |
| 4361 | | LLVMValueRef tag_value = bigint_to_llvm_const(get_llvm_type(g, union_type->data.unionation.tag_type), |
| 4362 | | &field->enum_field->value); |
| 4363 | | gen_store_untyped(g, tag_value, tag_field_ptr, 0, false); |
| 4364 | | } |
| 4374 | |
| 4375 | LLVMValueRef tag_value = bigint_to_llvm_const(tag_type_ref, |
| 4376 | &field->enum_field->value); |
| 4377 | assert(tag_field_ptr != nullptr); |
| 4378 | gen_store_untyped(g, tag_value, tag_field_ptr, 0, false); |
| 4379 | |
| 4365 | 4380 | return nullptr; |
| 4366 | 4381 | } |
| 4367 | 4382 | |