1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 11:38:21 +02:00

Fixed subtracting pointers to zero-sized elements

This commit is contained in:
Brian Fiete 2022-05-13 14:37:29 -07:00
parent 688c80e2fc
commit fa2cb7ba56

View file

@ -23317,11 +23317,20 @@ void BfExprEvaluator::PerformBinaryOperation(BfAstNode* leftExpression, BfAstNod
BfPointerType* resultPointerType = (BfPointerType*)resultType; BfPointerType* resultPointerType = (BfPointerType*)resultType;
BfType* intPtrType = mModule->GetPrimitiveType(BfTypeCode_IntPtr); BfType* intPtrType = mModule->GetPrimitiveType(BfTypeCode_IntPtr);
if (resultPointerType->mElementType->mSize == 0)
{
if (!mModule->IsInSpecializedSection())
mModule->Warn(0, "Subtracting pointers to zero-sized elements will always result in zero", opToken);
mResult = mModule->GetDefaultTypedValue(intPtrType);
}
else
{
convLeftValue = mModule->CastToValue(leftExpression, leftValue, intPtrType, (BfCastFlags)(BfCastFlags_Explicit | BfCastFlags_FromCompiler)); convLeftValue = mModule->CastToValue(leftExpression, leftValue, intPtrType, (BfCastFlags)(BfCastFlags_Explicit | BfCastFlags_FromCompiler));
convRightValue = mModule->CastToValue(rightExpression, rightValue, intPtrType, (BfCastFlags)(BfCastFlags_Explicit | BfCastFlags_FromCompiler)); convRightValue = mModule->CastToValue(rightExpression, rightValue, intPtrType, (BfCastFlags)(BfCastFlags_Explicit | BfCastFlags_FromCompiler));
BfIRValue diffValue = mModule->mBfIRBuilder->CreateSub(convLeftValue, convRightValue); BfIRValue diffValue = mModule->mBfIRBuilder->CreateSub(convLeftValue, convRightValue);
diffValue = mModule->mBfIRBuilder->CreateDiv(diffValue, mModule->GetConstValue(resultPointerType->mElementType->mSize, intPtrType), true); diffValue = mModule->mBfIRBuilder->CreateDiv(diffValue, mModule->GetConstValue(resultPointerType->mElementType->mSize, intPtrType), true);
mResult = BfTypedValue(diffValue, intPtrType); mResult = BfTypedValue(diffValue, intPtrType);
}
return; return;
} }
else if ((binaryOp != BfBinaryOp_Equality) && (binaryOp != BfBinaryOp_StrictEquality) && else if ((binaryOp != BfBinaryOp_Equality) && (binaryOp != BfBinaryOp_StrictEquality) &&
@ -23382,9 +23391,7 @@ void BfExprEvaluator::PerformBinaryOperation(BfAstNode* leftExpression, BfAstNod
if (underlyingType->IsValuelessType()) if (underlyingType->IsValuelessType())
{ {
if (!mModule->IsInSpecializedSection()) if (!mModule->IsInSpecializedSection())
{
mModule->Warn(0, "Adding to a pointer to a zero-sized element has no effect", opToken); mModule->Warn(0, "Adding to a pointer to a zero-sized element has no effect", opToken);
}
mResult = *resultTypedValue; mResult = *resultTypedValue;
return; return;
} }