1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-15 23:04:09 +02:00

Fix for new function/delegate casting

This commit is contained in:
Brian Fiete 2020-03-31 11:35:09 -07:00
parent 384863ae32
commit ca814fe695

View file

@ -9532,56 +9532,55 @@ BfTypedValue BfModule::Cast(BfAstNode* srcNode, const BfTypedValue& typedVal, Bf
// Function->Function and Delegate->Delegate where type is compatible but not exact // Function->Function and Delegate->Delegate where type is compatible but not exact
if (((typedVal.mType->IsDelegate()) || (typedVal.mType->IsFunction())) && if (((typedVal.mType->IsDelegate()) || (typedVal.mType->IsFunction())) &&
(typedVal.mType != toType) && // Don't bother to check for exact match, let CastToValue handle this
((typedVal.mType->IsDelegate()) == (toType->IsDelegate())) && ((typedVal.mType->IsDelegate()) == (toType->IsDelegate())) &&
((typedVal.mType->IsFunction()) == (toType->IsFunction()))) ((typedVal.mType->IsFunction()) == (toType->IsFunction())))
{ {
auto fromDelegateType = (BfDelegateType*)typedVal.mType->ToTypeInstance(); auto fromTypeInst = typedVal.mType->ToTypeInstance();
auto toDelegateType = (BfDelegateType*)toType; auto toTypeInst = toType->ToTypeInstance();
if (fromDelegateType->mReturnType == toDelegateType->mReturnType) auto fromMethodInst = GetRawMethodByName(fromTypeInst, "Invoke", -1, true);
auto toMethodInst = GetRawMethodByName(toTypeInst, "Invoke", -1, true);
if ((fromMethodInst != NULL) && (toMethodInst != NULL) &&
(fromMethodInst->mMethodDef->mCallingConvention == toMethodInst->mMethodDef->mCallingConvention) &&
(fromMethodInst->mReturnType == toMethodInst->mReturnType) &&
(fromMethodInst->GetParamCount() == toMethodInst->GetParamCount()))
{ {
auto fromMethodInst = GetRawMethodByName(fromDelegateType, "Invoke"); bool matched = true;
auto toMethodInst = GetRawMethodByName(toDelegateType, "Invoke");
if ((fromMethodInst->mMethodDef->mCallingConvention == toMethodInst->mMethodDef->mCallingConvention) && StringT<64> fromParamName;
(fromMethodInst->mReturnType == toMethodInst->mReturnType) && StringT<64> toParamName;
(fromMethodInst->GetParamCount() == toMethodInst->GetParamCount()))
for (int paramIdx = 0; paramIdx < (int)fromMethodInst->GetParamCount(); paramIdx++)
{ {
bool matched = true; bool nameMatches = true;
StringT<64> fromParamName; if (!explicitCast)
StringT<64> toParamName;
for (int paramIdx = 0; paramIdx < (int)fromMethodInst->GetParamCount(); paramIdx++)
{ {
bool nameMatches = true; fromMethodInst->GetParamName(paramIdx, fromParamName);
toMethodInst->GetParamName(paramIdx, toParamName);
if (!explicitCast) if ((!fromParamName.IsEmpty()) && (!toParamName.IsEmpty()))
{ nameMatches = fromParamName == toParamName;
fromMethodInst->GetParamName(paramIdx, fromParamName);
toMethodInst->GetParamName(paramIdx, toParamName);
if ((!fromParamName.IsEmpty()) && (!toParamName.IsEmpty()))
nameMatches = fromParamName == toParamName;
}
if ((fromMethodInst->GetParamKind(paramIdx) == toMethodInst->GetParamKind(paramIdx)) &&
(fromMethodInst->GetParamType(paramIdx) == toMethodInst->GetParamType(paramIdx)) &&
(nameMatches))
{
// Matched, required for implicit/explicit
}
else
{
matched = false;
break;
}
} }
if (matched) if ((fromMethodInst->GetParamKind(paramIdx) == toMethodInst->GetParamKind(paramIdx)) &&
(fromMethodInst->GetParamType(paramIdx) == toMethodInst->GetParamType(paramIdx)) &&
(nameMatches))
{ {
BfTypedValue loadedVal = LoadValue(typedVal); // Matched, required for implicit/explicit
return BfTypedValue(mBfIRBuilder->CreateBitCast(loadedVal.mValue, mBfIRBuilder->MapType(toType)), toType);
} }
else
{
matched = false;
break;
}
}
if (matched)
{
BfTypedValue loadedVal = LoadValue(typedVal);
return BfTypedValue(mBfIRBuilder->CreateBitCast(loadedVal.mValue, mBfIRBuilder->MapType(toType)), toType);
} }
} }
} }