@@ -480,8 +480,8 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionInitEnum *) {
480
480
return IrInstructionIdInitEnum;
481
481
}
482
482
483
- static constexpr IrInstructionId ir_instruction_id(IrInstructionBitCast *) {
484
- return IrInstructionIdBitCast ;
483
+ static constexpr IrInstructionId ir_instruction_id(IrInstructionPtrCast *) {
484
+ return IrInstructionIdPtrCast ;
485
485
}
486
486
487
487
static constexpr IrInstructionId ir_instruction_id(IrInstructionWidenOrShorten *) {
@@ -1940,16 +1940,16 @@ static IrInstruction *ir_build_init_enum_from(IrBuilder *irb, IrInstruction *old
1940
1940
return new_instruction;
1941
1941
}
1942
1942
1943
- static IrInstruction *ir_build_bit_cast (IrBuilder *irb, Scope *scope, AstNode *source_node,
1944
- IrInstruction *dest_type, IrInstruction *target )
1943
+ static IrInstruction *ir_build_ptr_cast (IrBuilder *irb, Scope *scope, AstNode *source_node,
1944
+ IrInstruction *dest_type, IrInstruction *ptr )
1945
1945
{
1946
- IrInstructionBitCast *instruction = ir_build_instruction<IrInstructionBitCast >(
1946
+ IrInstructionPtrCast *instruction = ir_build_instruction<IrInstructionPtrCast >(
1947
1947
irb, scope, source_node);
1948
1948
instruction->dest_type = dest_type;
1949
- instruction->target = target ;
1949
+ instruction->ptr = ptr ;
1950
1950
1951
1951
if (dest_type) ir_ref_instruction(dest_type, irb->current_basic_block);
1952
- ir_ref_instruction(target , irb->current_basic_block);
1952
+ ir_ref_instruction(ptr , irb->current_basic_block);
1953
1953
1954
1954
return &instruction->base;
1955
1955
}
@@ -2666,12 +2666,12 @@ static IrInstruction *ir_instruction_initenum_get_dep(IrInstructionInitEnum *ins
2666
2666
}
2667
2667
}
2668
2668
2669
- static IrInstruction *ir_instruction_bitcast_get_dep(IrInstructionBitCast *instruction,
2669
+ static IrInstruction *ir_instruction_ptrcast_get_dep(IrInstructionPtrCast *instruction,
2670
2670
size_t index)
2671
2671
{
2672
2672
switch (index) {
2673
2673
case 0: return instruction->dest_type;
2674
- case 1: return instruction->target ;
2674
+ case 1: return instruction->ptr ;
2675
2675
default: return nullptr;
2676
2676
}
2677
2677
}
@@ -2928,8 +2928,8 @@ static IrInstruction *ir_instruction_get_dep(IrInstruction *instruction, size_t
2928
2928
return ir_instruction_testcomptime_get_dep((IrInstructionTestComptime *) instruction, index);
2929
2929
case IrInstructionIdInitEnum:
2930
2930
return ir_instruction_initenum_get_dep((IrInstructionInitEnum *) instruction, index);
2931
- case IrInstructionIdBitCast :
2932
- return ir_instruction_bitcast_get_dep((IrInstructionBitCast *) instruction, index);
2931
+ case IrInstructionIdPtrCast :
2932
+ return ir_instruction_ptrcast_get_dep((IrInstructionPtrCast *) instruction, index);
2933
2933
case IrInstructionIdWidenOrShorten:
2934
2934
return ir_instruction_widenorshorten_get_dep((IrInstructionWidenOrShorten *) instruction, index);
2935
2935
case IrInstructionIdIntToPtr:
@@ -4200,7 +4200,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
4200
4200
4201
4201
return ir_build_panic(irb, scope, node, arg0_value);
4202
4202
}
4203
- case BuiltinFnIdBitCast :
4203
+ case BuiltinFnIdPtrCast :
4204
4204
{
4205
4205
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
4206
4206
IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
@@ -4212,7 +4212,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
4212
4212
if (arg1_value == irb->codegen->invalid_instruction)
4213
4213
return arg1_value;
4214
4214
4215
- return ir_build_bit_cast (irb, scope, node, arg0_value, arg1_value);
4215
+ return ir_build_ptr_cast (irb, scope, node, arg0_value, arg1_value);
4216
4216
}
4217
4217
}
4218
4218
zig_unreachable();
@@ -12182,34 +12182,36 @@ static TypeTableEntry *ir_analyze_instruction_panic(IrAnalyze *ira, IrInstructio
12182
12182
return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable);
12183
12183
}
12184
12184
12185
- static TypeTableEntry *ir_analyze_instruction_bit_cast(IrAnalyze *ira, IrInstructionBitCast *instruction) {
12185
+ static bool is_ptr_type(TypeTableEntry *t) {
12186
+ return (t->id == TypeTableEntryIdPointer || t->id == TypeTableEntryIdFn ||
12187
+ (t->id == TypeTableEntryIdMaybe && (t->data.maybe.child_type->id == TypeTableEntryIdPointer ||
12188
+ t->data.maybe.child_type->id == TypeTableEntryIdFn)));
12189
+ }
12190
+
12191
+ static TypeTableEntry *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstructionPtrCast *instruction) {
12186
12192
IrInstruction *dest_type_value = instruction->dest_type->other;
12187
12193
TypeTableEntry *dest_type = ir_resolve_type(ira, dest_type_value);
12188
12194
if (type_is_invalid(dest_type))
12189
12195
return ira->codegen->builtin_types.entry_invalid;
12190
12196
12191
- IrInstruction *target = instruction->target ->other;
12192
- TypeTableEntry *src_type = target ->value.type;
12197
+ IrInstruction *ptr = instruction->ptr ->other;
12198
+ TypeTableEntry *src_type = ptr ->value.type;
12193
12199
if (type_is_invalid(src_type))
12194
12200
return ira->codegen->builtin_types.entry_invalid;
12195
12201
12196
- ensure_complete_type(ira->codegen, dest_type);
12197
- ensure_complete_type(ira->codegen, src_type);
12202
+ if (!is_ptr_type(src_type)) {
12203
+ ir_add_error(ira, ptr, buf_sprintf("expected pointer, found '%s'", buf_ptr(&src_type->name)));
12204
+ return ira->codegen->builtin_types.entry_invalid;
12205
+ }
12198
12206
12199
- uint64_t dest_size_bytes = type_size(ira->codegen, dest_type);
12200
- uint64_t src_size_bytes = type_size(ira->codegen, src_type);
12201
- if (dest_size_bytes != src_size_bytes) {
12202
- ir_add_error(ira, &instruction->base,
12203
- buf_sprintf("destination type '%s' has size %" PRIu64 " but source type '%s' has size %" PRIu64,
12204
- buf_ptr(&dest_type->name), dest_size_bytes,
12205
- buf_ptr(&src_type->name), src_size_bytes));
12207
+ if (!is_ptr_type(dest_type)) {
12208
+ ir_add_error(ira, dest_type_value,
12209
+ buf_sprintf("expected pointer, found '%s'", buf_ptr(&dest_type->name)));
12206
12210
return ira->codegen->builtin_types.entry_invalid;
12207
12211
}
12208
12212
12209
- if (instr_is_comptime(target) && src_type->id == dest_type->id &&
12210
- (src_type->id == TypeTableEntryIdPointer || src_type->id == TypeTableEntryIdMaybe))
12211
- {
12212
- ConstExprValue *val = ir_resolve_const(ira, target, UndefOk);
12213
+ if (instr_is_comptime(ptr)) {
12214
+ ConstExprValue *val = ir_resolve_const(ira, ptr, UndefOk);
12213
12215
if (!val)
12214
12216
return ira->codegen->builtin_types.entry_invalid;
12215
12217
@@ -12219,8 +12221,8 @@ static TypeTableEntry *ir_analyze_instruction_bit_cast(IrAnalyze *ira, IrInstruc
12219
12221
return dest_type;
12220
12222
}
12221
12223
12222
- IrInstruction *result = ir_build_bit_cast (&ira->new_irb, instruction->base.scope,
12223
- instruction->base.source_node, nullptr, target );
12224
+ IrInstruction *result = ir_build_ptr_cast (&ira->new_irb, instruction->base.scope,
12225
+ instruction->base.source_node, nullptr, ptr );
12224
12226
ir_link_new_instruction(result, &instruction->base);
12225
12227
result->value.type = dest_type;
12226
12228
return dest_type;
@@ -12464,8 +12466,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
12464
12466
return ir_analyze_instruction_decl_ref(ira, (IrInstructionDeclRef *)instruction);
12465
12467
case IrInstructionIdPanic:
12466
12468
return ir_analyze_instruction_panic(ira, (IrInstructionPanic *)instruction);
12467
- case IrInstructionIdBitCast :
12468
- return ir_analyze_instruction_bit_cast (ira, (IrInstructionBitCast *)instruction);
12469
+ case IrInstructionIdPtrCast :
12470
+ return ir_analyze_instruction_ptr_cast (ira, (IrInstructionPtrCast *)instruction);
12469
12471
case IrInstructionIdMaybeWrap:
12470
12472
case IrInstructionIdErrWrapCode:
12471
12473
case IrInstructionIdErrWrapPayload:
@@ -12637,7 +12639,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
12637
12639
case IrInstructionIdFnProto:
12638
12640
case IrInstructionIdTestComptime:
12639
12641
case IrInstructionIdInitEnum:
12640
- case IrInstructionIdBitCast :
12642
+ case IrInstructionIdPtrCast :
12641
12643
case IrInstructionIdWidenOrShorten:
12642
12644
case IrInstructionIdPtrToInt:
12643
12645
case IrInstructionIdIntToPtr:
0 commit comments