mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-08 11:38:21 +02:00
Support for non-static ++ and -- operator overloads
This commit is contained in:
parent
5924d4819b
commit
66d5f67528
3 changed files with 58 additions and 19 deletions
|
@ -521,10 +521,13 @@ BfMethodDef* BfDefBuilder::CreateMethodDef(BfMethodDeclaration* methodDeclaratio
|
|||
{
|
||||
if (!methodDef->mIsStatic)
|
||||
{
|
||||
if (!declError.empty())
|
||||
declError += " and ";
|
||||
declError += "'static'";
|
||||
methodDef->mIsStatic = true; // Fix it
|
||||
if ((operatorDecl->mUnaryOp != BfUnaryOp_Increment) && (operatorDecl->mUnaryOp != BfUnaryOp_Decrement))
|
||||
{
|
||||
if (!declError.empty())
|
||||
declError += " and ";
|
||||
declError += "'static'";
|
||||
methodDef->mIsStatic = true; // Fix it
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!declError.empty())
|
||||
|
|
|
@ -17721,8 +17721,16 @@ BfTypedValue BfExprEvaluator::PerformUnaryOperation_TryOperator(const BfTypedVal
|
|||
{
|
||||
if (!methodMatcher.IsMemberAccessible(checkType, operatorDef->mDeclaringType))
|
||||
continue;
|
||||
|
||||
int prevArgSize = (int)args.mSize;
|
||||
if (!operatorDef->mIsStatic)
|
||||
{
|
||||
// Try without arg
|
||||
args.mSize = 0;
|
||||
}
|
||||
if (methodMatcher.CheckMethod(NULL, checkType, operatorDef, false))
|
||||
methodMatcher.mSelfType = entry.mSrcType;
|
||||
args.mSize = prevArgSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17786,13 +17794,26 @@ BfTypedValue BfExprEvaluator::PerformUnaryOperation_TryOperator(const BfTypedVal
|
|||
SizedArray<BfExpression*, 2> argSrcs;
|
||||
argSrcs.push_back(unaryOpExpr);
|
||||
|
||||
BfTypedValue targetVal = args[0].mTypedValue;
|
||||
BfTypedValue postOpVal;
|
||||
if (isPostOp)
|
||||
postOpVal = mModule->LoadValue(args[0].mTypedValue);
|
||||
if (isPostOp)
|
||||
postOpVal = mModule->LoadValue(targetVal);
|
||||
|
||||
BfTypedValue callTarget;
|
||||
if (!methodMatcher.mBestMethodDef->mIsStatic)
|
||||
{
|
||||
callTarget = targetVal;
|
||||
args.Clear();
|
||||
}
|
||||
|
||||
auto result = CreateCall(&methodMatcher, BfTypedValue());
|
||||
auto result = CreateCall(&methodMatcher, callTarget);
|
||||
|
||||
if ((result.mType != NULL) && (methodMatcher.mSelfType != NULL) && (result.mType->IsSelf()))
|
||||
if (!methodMatcher.mBestMethodDef->mIsStatic)
|
||||
{
|
||||
if (!isPostOp)
|
||||
result = mModule->LoadValue(targetVal);
|
||||
}
|
||||
else if ((result.mType != NULL) && (methodMatcher.mSelfType != NULL) && (result.mType->IsSelf()))
|
||||
{
|
||||
BF_ASSERT(mModule->IsInGeneric());
|
||||
result = mModule->GetDefaultTypedValue(methodMatcher.mSelfType);
|
||||
|
|
|
@ -17002,20 +17002,35 @@ void BfModule::ProcessMethod(BfMethodInstance* methodInstance, bool isInlineDup)
|
|||
}
|
||||
else if (operatorDef->mOperatorDeclaration->mUnaryOp != BfUnaryOp_None)
|
||||
{
|
||||
if (methodDef->mParams.size() != 1)
|
||||
if (methodDef->mIsStatic)
|
||||
{
|
||||
Fail("Unary operators must declare one parameter", paramErrorRefNode);
|
||||
}
|
||||
else if ((mCurMethodInstance->GetParamType(0) != mCurTypeInstance) && (!mCurMethodInstance->GetParamType(0)->IsSelf()))
|
||||
{
|
||||
Fail("The parameter of a unary operator must be the containing type", paramErrorRefNode);
|
||||
}
|
||||
if (methodDef->mParams.size() != 1)
|
||||
{
|
||||
Fail("Unary operators must declare one parameter", paramErrorRefNode);
|
||||
}
|
||||
else if ((mCurMethodInstance->GetParamType(0) != mCurTypeInstance) && (!mCurMethodInstance->GetParamType(0)->IsSelf()))
|
||||
{
|
||||
Fail("The parameter of a unary operator must be the containing type", paramErrorRefNode);
|
||||
}
|
||||
|
||||
if (((operatorDef->mOperatorDeclaration->mUnaryOp == BfUnaryOp_Increment) ||
|
||||
(operatorDef->mOperatorDeclaration->mUnaryOp == BfUnaryOp_Decrement)) &&
|
||||
(!TypeIsSubTypeOf(mCurMethodInstance->mReturnType->ToTypeInstance(), mCurTypeInstance)))
|
||||
if (((operatorDef->mOperatorDeclaration->mUnaryOp == BfUnaryOp_Increment) ||
|
||||
(operatorDef->mOperatorDeclaration->mUnaryOp == BfUnaryOp_Decrement)) &&
|
||||
(!TypeIsSubTypeOf(mCurMethodInstance->mReturnType->ToTypeInstance(), mCurTypeInstance)))
|
||||
{
|
||||
Fail("The return type for the '++' or '--' operator must match the parameter type or be derived from the parameter type", operatorDef->mOperatorDeclaration->mReturnType);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Fail("The return type for the '++' or '--' operator must match the parameter type or be derived from the parameter type", operatorDef->mOperatorDeclaration->mReturnType);
|
||||
if (methodDef->mParams.size() != 0)
|
||||
{
|
||||
Fail("Non-static unary operators not declare any parameters", paramErrorRefNode);
|
||||
}
|
||||
|
||||
if (!mCurMethodInstance->mReturnType->IsVoid())
|
||||
{
|
||||
Fail("The return type for non-static operator must be 'void'", operatorDef->mOperatorDeclaration->mReturnType);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (operatorDef->mOperatorDeclaration->mAssignOp != BfAssignmentOp_None)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue