Quellcode durchsuchen

Allow <=> on bools

Brian Fiete vor 3 Jahren
Ursprung
Commit
252fe13a5d

+ 5 - 0
BeefLibs/corlib/src/Boolean.bf

@@ -17,6 +17,11 @@ namespace System
 		    strBuffer.Append(((bool)this) ? TrueString : FalseString);
 		}
 
+		public static int operator<=>(Boolean a, Boolean b)
+		{
+			return (SelfBase)a <=> (SelfBase)b;
+		}
+
 		public int GetHashCode()
 		{
 			return ((bool)this) ? 1 : 0;

+ 9 - 4
IDEHelper/Compiler/BfExprEvaluator.cpp

@@ -22477,6 +22477,7 @@ void BfExprEvaluator::PerformBinaryOperation(BfType* resultType, BfIRValue convL
 		auto primType = (BfPrimitiveType*)resultType;
 		if (primType->mTypeDef->mTypeCode == BfTypeCode_Boolean)
 		{
+			bool passThrough = false;
 			switch (binaryOp)
 			{
 			case BfBinaryOp_Equality:
@@ -22503,16 +22504,20 @@ void BfExprEvaluator::PerformBinaryOperation(BfType* resultType, BfIRValue convL
 				mResult = BfTypedValue(mModule->mBfIRBuilder->CreateXor(convLeftValue, convRightValue),
 					mModule->GetPrimitiveType(BfTypeCode_Boolean));
 				break;
+			case BfBinaryOp_Compare:
+				passThrough = true;
+				break;
 			default:
 				if (mModule->PreFail())
 					mModule->Fail("Invalid operation for booleans", opToken);
 				break;
 			}
-			return;
+			if (!passThrough)
+				return;
 		}
 	}
 
-	if ((!resultType->IsIntegral()) && (!resultType->IsFloat()))
+	if ((!resultType->IsIntegralOrBool()) && (!resultType->IsFloat()))
 	{
 		if (mModule->PreFail())
 			mModule->Fail(StrFormat("Cannot perform operation on type '%s'", mModule->TypeToString(resultType).c_str()), opToken);
@@ -22617,7 +22622,7 @@ void BfExprEvaluator::PerformBinaryOperation(BfType* resultType, BfIRValue convL
 		mResult = BfTypedValue(mModule->mBfIRBuilder->CreateCmpGTE(convLeftValue, convRightValue, resultType->IsSigned()),
 			mModule->GetPrimitiveType(BfTypeCode_Boolean));	
 		break;
-	case BfBinaryOp_Compare:				
+	case BfBinaryOp_Compare:
 		{
 			auto intType = mModule->GetPrimitiveType(BfTypeCode_IntPtr);
 			if ((convLeftValue.IsConst()) && (convRightValue.IsConst()))
@@ -22638,7 +22643,7 @@ void BfExprEvaluator::PerformBinaryOperation(BfType* resultType, BfIRValue convL
 						mResult = BfTypedValue(mModule->GetConstValue(0, mModule->GetPrimitiveType(BfTypeCode_IntPtr)), intType);
 				}
 			}
-			else if ((resultType->IsIntegral()) && (resultType->mSize < intType->mSize))
+			else if ((resultType->IsIntegralOrBool()) && (resultType->mSize < intType->mSize))
 			{
 				auto leftIntValue = mModule->mBfIRBuilder->CreateNumericCast(convLeftValue, resultType->IsSigned(), BfTypeCode_IntPtr);
 				auto rightIntValue = mModule->mBfIRBuilder->CreateNumericCast(convRightValue, resultType->IsSigned(), BfTypeCode_IntPtr);							

+ 2 - 0
IDEHelper/Compiler/BfResolvedTypeUtils.h

@@ -571,6 +571,7 @@ public:
 	virtual bool IsBoolean() { return false; }
 	virtual bool IsInteger() { return false; }
 	virtual bool IsIntegral() { return false; }
+	virtual bool IsIntegralOrBool() { return false; }
 	virtual bool IsIntPtr() { return false; }
 	virtual bool IsSigned() { return false; }
 	virtual bool IsSignedInt() { return false; }
@@ -627,6 +628,7 @@ public:
 	virtual bool IsValueTypeOrValueTypePtr() override { return true; }
 	virtual bool IsBoolean() override { return mTypeDef->mTypeCode == BfTypeCode_Boolean; }
 	virtual bool IsIntegral() override { return (mTypeDef->mTypeCode >= BfTypeCode_Int8) && (mTypeDef->mTypeCode <= BfTypeCode_Char32); }
+	virtual bool IsIntegralOrBool() override { return (mTypeDef->mTypeCode >= BfTypeCode_Boolean) && (mTypeDef->mTypeCode <= BfTypeCode_Char32); }
 	virtual bool IsInteger() override { return (mTypeDef->mTypeCode >= BfTypeCode_Int8) && (mTypeDef->mTypeCode <= BfTypeCode_UIntUnknown); }	
 	virtual bool IsIntPtr() override { return (mTypeDef->mTypeCode == BfTypeCode_IntPtr) || (mTypeDef->mTypeCode == BfTypeCode_UIntPtr); }
 	virtual bool IsIntPtrable() override