| ... | ... | @@ -185,8 +185,6 @@ pub fn analyzeBody( |
| 185 | 185 | //.block => try sema.zirBlock(block, inst), |
| 186 | 186 | //.suspend_block => try sema.zirSuspendBlock(block, inst), |
| 187 | 187 | //.bool_not => try sema.zirBoolNot(block, inst), |
| 188 | | //.bool_and => try sema.zirBoolOp(block, inst, false), |
| 189 | | //.bool_or => try sema.zirBoolOp(block, inst, true), |
| 190 | 188 | //.bool_br_and => try sema.zirBoolBr(block, inst, false), |
| 191 | 189 | //.bool_br_or => try sema.zirBoolBr(block, inst, true), |
| 192 | 190 | //.c_import => try sema.zirCImport(block, inst), |
| ... | ... | @@ -195,12 +193,12 @@ pub fn analyzeBody( |
| 195 | 193 | //.call_compile_time => try sema.zirCall(block, inst, .compile_time, false), |
| 196 | 194 | //.call_nosuspend => try sema.zirCall(block, inst, .no_async, false), |
| 197 | 195 | //.call_async => try sema.zirCall(block, inst, .async_kw, false), |
| 198 | | //.cmp_eq => try sema.zirCmp(block, inst, .eq), |
| 199 | | //.cmp_gt => try sema.zirCmp(block, inst, .gt), |
| 200 | | //.cmp_gte => try sema.zirCmp(block, inst, .gte), |
| 201 | | //.cmp_lt => try sema.zirCmp(block, inst, .lt), |
| 202 | | //.cmp_lte => try sema.zirCmp(block, inst, .lte), |
| 203 | | //.cmp_neq => try sema.zirCmp(block, inst, .neq), |
| 196 | .cmp_eq => try sema.zirCmp(block, inst, .eq), |
| 197 | .cmp_gt => try sema.zirCmp(block, inst, .gt), |
| 198 | .cmp_gte => try sema.zirCmp(block, inst, .gte), |
| 199 | .cmp_lt => try sema.zirCmp(block, inst, .lt), |
| 200 | .cmp_lte => try sema.zirCmp(block, inst, .lte), |
| 201 | .cmp_neq => try sema.zirCmp(block, inst, .neq), |
| 204 | 202 | //.coerce_result_ptr => try sema.zirCoerceResultPtr(block, inst), |
| 205 | 203 | //.decl_ref => try sema.zirDeclRef(block, inst), |
| 206 | 204 | //.decl_val => try sema.zirDeclVal(block, inst), |
| ... | ... | @@ -4669,8 +4667,8 @@ fn zirBitwise( |
| 4669 | 4667 | return sema.mod.fail(&block.base, src, "invalid operands to binary bitwise expression: '{s}' and '{s}'", .{ @tagName(lhs_ty.zigTypeTag()), @tagName(rhs_ty.zigTypeTag()) }); |
| 4670 | 4668 | } |
| 4671 | 4669 | |
| 4672 | | if (casted_lhs.value()) |lhs_val| { |
| 4673 | | if (casted_rhs.value()) |rhs_val| { |
| 4670 | if (try sema.resolvePossiblyUndefinedValue(block, lhs_src, casted_lhs)) |lhs_val| { |
| 4671 | if (try sema.resolvePossiblyUndefinedValue(block, rhs_src, casted_rhs)) |rhs_val| { |
| 4674 | 4672 | if (lhs_val.isUndef() or rhs_val.isUndef()) { |
| 4675 | 4673 | return sema.addConstUndef(resolved_type); |
| 4676 | 4674 | } |
| ... | ... | @@ -4799,8 +4797,8 @@ fn analyzeArithmetic( |
| 4799 | 4797 | return sema.mod.fail(&block.base, src, "invalid operands to binary expression: '{s}' and '{s}'", .{ @tagName(lhs_ty.zigTypeTag()), @tagName(rhs_ty.zigTypeTag()) }); |
| 4800 | 4798 | } |
| 4801 | 4799 | |
| 4802 | | if (casted_lhs.value()) |lhs_val| { |
| 4803 | | if (casted_rhs.value()) |rhs_val| { |
| 4800 | if (try sema.resolvePossiblyUndefinedValue(block, lhs_src, casted_lhs)) |lhs_val| { |
| 4801 | if (try sema.resolvePossiblyUndefinedValue(block, rhs_src, casted_rhs)) |rhs_val| { |
| 4804 | 4802 | if (lhs_val.isUndef() or rhs_val.isUndef()) { |
| 4805 | 4803 | return sema.addConstUndef(resolved_type); |
| 4806 | 4804 | } |
| ... | ... | @@ -5072,8 +5070,8 @@ fn zirCmp( |
| 5072 | 5070 | const casted_lhs = try sema.coerce(block, resolved_type, lhs, lhs_src); |
| 5073 | 5071 | const casted_rhs = try sema.coerce(block, resolved_type, rhs, rhs_src); |
| 5074 | 5072 | |
| 5075 | | if (casted_lhs.value()) |lhs_val| { |
| 5076 | | if (casted_rhs.value()) |rhs_val| { |
| 5073 | if (try sema.resolvePossiblyUndefinedValue(block, lhs_src, casted_lhs)) |lhs_val| { |
| 5074 | if (try sema.resolvePossiblyUndefinedValue(block, rhs_src, casted_rhs)) |rhs_val| { |
| 5077 | 5075 | if (lhs_val.isUndef() or rhs_val.isUndef()) { |
| 5078 | 5076 | return sema.addConstUndef(resolved_type); |
| 5079 | 5077 | } |
| ... | ... | @@ -5258,45 +5256,6 @@ fn zirBoolNot(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileErr |
| 5258 | 5256 | return block.addTyOp(.not, bool_type, operand); |
| 5259 | 5257 | } |
| 5260 | 5258 | |
| 5261 | | fn zirBoolOp( |
| 5262 | | sema: *Sema, |
| 5263 | | block: *Scope.Block, |
| 5264 | | inst: Zir.Inst.Index, |
| 5265 | | is_bool_or: bool, |
| 5266 | | ) CompileError!Air.Inst.Ref { |
| 5267 | | const tracy = trace(@src()); |
| 5268 | | defer tracy.end(); |
| 5269 | | |
| 5270 | | const src: LazySrcLoc = .unneeded; |
| 5271 | | const bool_type = Type.initTag(.bool); |
| 5272 | | const bin_inst = sema.code.instructions.items(.data)[inst].bin; |
| 5273 | | const uncasted_lhs = sema.resolveInst(bin_inst.lhs); |
| 5274 | | const lhs = try sema.coerce(block, bool_type, uncasted_lhs, uncasted_lhs.src); |
| 5275 | | const uncasted_rhs = sema.resolveInst(bin_inst.rhs); |
| 5276 | | const rhs = try sema.coerce(block, bool_type, uncasted_rhs, uncasted_rhs.src); |
| 5277 | | |
| 5278 | | if (lhs.value()) |lhs_val| { |
| 5279 | | if (rhs.value()) |rhs_val| { |
| 5280 | | if (is_bool_or) { |
| 5281 | | if (lhs_val.toBool() or rhs_val.toBool()) { |
| 5282 | | return Air.Inst.Ref.bool_true; |
| 5283 | | } else { |
| 5284 | | return Air.Inst.Ref.bool_false; |
| 5285 | | } |
| 5286 | | } else { |
| 5287 | | if (lhs_val.toBool() and rhs_val.toBool()) { |
| 5288 | | return Air.Inst.Ref.bool_true; |
| 5289 | | } else { |
| 5290 | | return Air.Inst.Ref.bool_false; |
| 5291 | | } |
| 5292 | | } |
| 5293 | | } |
| 5294 | | } |
| 5295 | | try sema.requireRuntimeBlock(block, src); |
| 5296 | | const tag: Air.Inst.Tag = if (is_bool_or) .bool_or else .bool_and; |
| 5297 | | return block.addBinOp(tag, lhs, rhs); |
| 5298 | | } |
| 5299 | | |
| 5300 | 5259 | fn zirBoolBr( |
| 5301 | 5260 | sema: *Sema, |
| 5302 | 5261 | parent_block: *Scope.Block, |
| ... | ... | @@ -6840,8 +6799,8 @@ fn elemPtrArray( |
| 6840 | 6799 | elem_index: Air.Inst.Ref, |
| 6841 | 6800 | elem_index_src: LazySrcLoc, |
| 6842 | 6801 | ) CompileError!Air.Inst.Ref { |
| 6843 | | if (array_ptr.value()) |array_ptr_val| { |
| 6844 | | if (elem_index.value()) |index_val| { |
| 6802 | if (try sema.resolveDefinedValue(block, src, array_ptr)) |array_ptr_val| { |
| 6803 | if (try sema.resolveDefinedValue(block, src, elem_index)) |index_val| { |
| 6845 | 6804 | // Both array pointer and index are compile-time known. |
| 6846 | 6805 | const index_u64 = index_val.toUnsignedInt(); |
| 6847 | 6806 | // @intCast here because it would have been impossible to construct a value that |
| ... | ... | @@ -7367,8 +7326,8 @@ fn analyzeSlice( |
| 7367 | 7326 | var return_ptr_size: std.builtin.TypeInfo.Pointer.Size = .Slice; |
| 7368 | 7327 | var return_elem_type = elem_type; |
| 7369 | 7328 | if (end_opt) |end| { |
| 7370 | | if (end.value()) |end_val| { |
| 7371 | | if (start.value()) |start_val| { |
| 7329 | if (try sema.resolveDefinedValue(block, src, end)) |end_val| { |
| 7330 | if (try sema.resolveDefinedValue(block, src, start)) |start_val| { |
| 7372 | 7331 | const start_u64 = start_val.toUnsignedInt(); |
| 7373 | 7332 | const end_u64 = end_val.toUnsignedInt(); |
| 7374 | 7333 | if (start_u64 > end_u64) { |
| ... | ... | @@ -7492,11 +7451,11 @@ fn cmpNumeric( |
| 7492 | 7451 | // For mixed floats and integers, extract the integer part from the float, cast that to |
| 7493 | 7452 | // a signed integer with mantissa bits + 1, and if there was any non-integral part of the float, |
| 7494 | 7453 | // add/subtract 1. |
| 7495 | | const lhs_is_signed = if (lhs.value()) |lhs_val| |
| 7454 | const lhs_is_signed = if (try sema.resolveDefinedValue(block, lhs_src, lhs)) |lhs_val| |
| 7496 | 7455 | lhs_val.compareWithZero(.lt) |
| 7497 | 7456 | else |
| 7498 | 7457 | (lhs_ty.isFloat() or lhs_ty.isSignedInt()); |
| 7499 | | const rhs_is_signed = if (rhs.value()) |rhs_val| |
| 7458 | const rhs_is_signed = if (try sema.resolveDefinedValue(block, rhs_src, rhs)) |rhs_val| |
| 7500 | 7459 | rhs_val.compareWithZero(.lt) |
| 7501 | 7460 | else |
| 7502 | 7461 | (rhs_ty.isFloat() or rhs_ty.isSignedInt()); |
| ... | ... | @@ -7505,7 +7464,7 @@ fn cmpNumeric( |
| 7505 | 7464 | var dest_float_type: ?Type = null; |
| 7506 | 7465 | |
| 7507 | 7466 | var lhs_bits: usize = undefined; |
| 7508 | | if (lhs.value()) |lhs_val| { |
| 7467 | if (try sema.resolvePossiblyUndefinedValue(block, lhs_src, lhs)) |lhs_val| { |
| 7509 | 7468 | if (lhs_val.isUndef()) |
| 7510 | 7469 | return sema.addConstUndef(Type.initTag(.bool)); |
| 7511 | 7470 | const is_unsigned = if (lhs_is_float) x: { |
| ... | ... | @@ -7540,7 +7499,7 @@ fn cmpNumeric( |
| 7540 | 7499 | } |
| 7541 | 7500 | |
| 7542 | 7501 | var rhs_bits: usize = undefined; |
| 7543 | | if (rhs.value()) |rhs_val| { |
| 7502 | if (try sema.resolvePossiblyUndefinedValue(block, rhs_src, rhs)) |rhs_val| { |
| 7544 | 7503 | if (rhs_val.isUndef()) |
| 7545 | 7504 | return sema.addConstUndef(Type.initTag(.bool)); |
| 7546 | 7505 | const is_unsigned = if (rhs_is_float) x: { |
| ... | ... | @@ -7589,8 +7548,8 @@ fn cmpNumeric( |
| 7589 | 7548 | } |
| 7590 | 7549 | |
| 7591 | 7550 | fn wrapOptional(sema: *Sema, block: *Scope.Block, dest_type: Type, inst: Air.Inst.Ref) !Air.Inst.Index { |
| 7592 | | if (inst.value()) |val| { |
| 7593 | | return sema.mod.constInst(sema.arena, inst.src, .{ .ty = dest_type, .val = val }); |
| 7551 | if (try sema.resolvePossiblyUndefinedValue(block, inst_src, inst)) |val| { |
| 7552 | return sema.mod.constInst(sema.arena, inst_src, .{ .ty = dest_type, .val = val }); |
| 7594 | 7553 | } |
| 7595 | 7554 | |
| 7596 | 7555 | try sema.requireRuntimeBlock(block, inst.src); |
| ... | ... | @@ -7690,67 +7649,69 @@ fn resolvePeerTypes( |
| 7690 | 7649 | |
| 7691 | 7650 | var chosen = instructions[0]; |
| 7692 | 7651 | for (instructions[1..]) |candidate| { |
| 7693 | | if (candidate.ty.eql(chosen.ty)) |
| 7652 | const candidate_ty = sema.getTypeOf(candidate); |
| 7653 | const chosen_ty = sema.getTypeOf(chosen); |
| 7654 | if (candidate_ty.eql(chosen_ty)) |
| 7694 | 7655 | continue; |
| 7695 | | if (candidate.ty.zigTypeTag() == .NoReturn) |
| 7656 | if (candidate_ty.zigTypeTag() == .NoReturn) |
| 7696 | 7657 | continue; |
| 7697 | | if (chosen.ty.zigTypeTag() == .NoReturn) { |
| 7658 | if (chosen_ty.zigTypeTag() == .NoReturn) { |
| 7698 | 7659 | chosen = candidate; |
| 7699 | 7660 | continue; |
| 7700 | 7661 | } |
| 7701 | | if (candidate.ty.zigTypeTag() == .Undefined) |
| 7662 | if (candidate_ty.zigTypeTag() == .Undefined) |
| 7702 | 7663 | continue; |
| 7703 | | if (chosen.ty.zigTypeTag() == .Undefined) { |
| 7664 | if (chosen_ty.zigTypeTag() == .Undefined) { |
| 7704 | 7665 | chosen = candidate; |
| 7705 | 7666 | continue; |
| 7706 | 7667 | } |
| 7707 | | if (chosen.ty.isInt() and |
| 7708 | | candidate.ty.isInt() and |
| 7709 | | chosen.ty.isSignedInt() == candidate.ty.isSignedInt()) |
| 7668 | if (chosen_ty.isInt() and |
| 7669 | candidate_ty.isInt() and |
| 7670 | chosen_ty.isSignedInt() == candidate_ty.isSignedInt()) |
| 7710 | 7671 | { |
| 7711 | | if (chosen.ty.intInfo(target).bits < candidate.ty.intInfo(target).bits) { |
| 7672 | if (chosen_ty.intInfo(target).bits < candidate_ty.intInfo(target).bits) { |
| 7712 | 7673 | chosen = candidate; |
| 7713 | 7674 | } |
| 7714 | 7675 | continue; |
| 7715 | 7676 | } |
| 7716 | | if (chosen.ty.isFloat() and candidate.ty.isFloat()) { |
| 7717 | | if (chosen.ty.floatBits(target) < candidate.ty.floatBits(target)) { |
| 7677 | if (chosen_ty.isFloat() and candidate_ty.isFloat()) { |
| 7678 | if (chosen_ty.floatBits(target) < candidate_ty.floatBits(target)) { |
| 7718 | 7679 | chosen = candidate; |
| 7719 | 7680 | } |
| 7720 | 7681 | continue; |
| 7721 | 7682 | } |
| 7722 | 7683 | |
| 7723 | | if (chosen.ty.zigTypeTag() == .ComptimeInt and candidate.ty.isInt()) { |
| 7684 | if (chosen_ty.zigTypeTag() == .ComptimeInt and candidate_ty.isInt()) { |
| 7724 | 7685 | chosen = candidate; |
| 7725 | 7686 | continue; |
| 7726 | 7687 | } |
| 7727 | 7688 | |
| 7728 | | if (chosen.ty.isInt() and candidate.ty.zigTypeTag() == .ComptimeInt) { |
| 7689 | if (chosen_ty.isInt() and candidate_ty.zigTypeTag() == .ComptimeInt) { |
| 7729 | 7690 | continue; |
| 7730 | 7691 | } |
| 7731 | 7692 | |
| 7732 | | if (chosen.ty.zigTypeTag() == .ComptimeFloat and candidate.ty.isFloat()) { |
| 7693 | if (chosen_ty.zigTypeTag() == .ComptimeFloat and candidate_ty.isFloat()) { |
| 7733 | 7694 | chosen = candidate; |
| 7734 | 7695 | continue; |
| 7735 | 7696 | } |
| 7736 | 7697 | |
| 7737 | | if (chosen.ty.isFloat() and candidate.ty.zigTypeTag() == .ComptimeFloat) { |
| 7698 | if (chosen_ty.isFloat() and candidate_ty.zigTypeTag() == .ComptimeFloat) { |
| 7738 | 7699 | continue; |
| 7739 | 7700 | } |
| 7740 | 7701 | |
| 7741 | | if (chosen.ty.zigTypeTag() == .Enum and candidate.ty.zigTypeTag() == .EnumLiteral) { |
| 7702 | if (chosen_ty.zigTypeTag() == .Enum and candidate_ty.zigTypeTag() == .EnumLiteral) { |
| 7742 | 7703 | continue; |
| 7743 | 7704 | } |
| 7744 | | if (chosen.ty.zigTypeTag() == .EnumLiteral and candidate.ty.zigTypeTag() == .Enum) { |
| 7705 | if (chosen_ty.zigTypeTag() == .EnumLiteral and candidate_ty.zigTypeTag() == .Enum) { |
| 7745 | 7706 | chosen = candidate; |
| 7746 | 7707 | continue; |
| 7747 | 7708 | } |
| 7748 | 7709 | |
| 7749 | 7710 | // TODO error notes pointing out each type |
| 7750 | | return sema.mod.fail(&block.base, src, "incompatible types: '{}' and '{}'", .{ chosen.ty, candidate.ty }); |
| 7711 | return sema.mod.fail(&block.base, src, "incompatible types: '{}' and '{}'", .{ chosen_ty, candidate_ty }); |
| 7751 | 7712 | } |
| 7752 | 7713 | |
| 7753 | | return chosen.ty; |
| 7714 | return sema.getTypeOf(chosen); |
| 7754 | 7715 | } |
| 7755 | 7716 | |
| 7756 | 7717 | fn resolveTypeFields(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, ty: Type) CompileError!Type { |