| 13319 | } |
| 13320 | |
| 13321 | BfIRValue BfModule::CastToFunction(BfAstNode* srcNode, const BfTypedValue& targetValue, BfMethodInstance* methodInstance, BfType* toType, BfCastFlags castFlags, BfIRValue irFunc) |
| 13322 | { |
| 13323 | auto invokeMethodInstance = GetDelegateInvokeMethod(toType->ToTypeInstance()); |
| 13324 | |
| 13325 | bool methodsThisMatch = true; |
| 13326 | if (invokeMethodInstance->mMethodDef->mIsStatic != methodInstance->mMethodDef->mIsStatic) |
| 13327 | methodsThisMatch = false; |
| 13328 | else |
| 13329 | { |
| 13330 | if (!methodInstance->mMethodDef->mIsStatic) |
| 13331 | { |
| 13332 | BfType* thisType = methodInstance->GetThisType(); |
| 13333 | if (thisType->IsPointer()) |
| 13334 | thisType = thisType->GetUnderlyingType(); |
| 13335 | BfType* invokeThisType = invokeMethodInstance->GetThisType(); |
| 13336 | if (invokeThisType->IsPointer()) |
| 13337 | invokeThisType = invokeThisType->GetUnderlyingType(); |
| 13338 | if (!TypeIsSubTypeOf(thisType->ToTypeInstance(), invokeThisType->ToTypeInstance())) |
| 13339 | methodsThisMatch = false; |
| 13340 | } |
| 13341 | } |
| 13342 | |
| 13343 | bool methodMatches = methodsThisMatch; |
| 13344 | if (methodMatches) |
| 13345 | methodMatches = invokeMethodInstance->IsExactMatch(methodInstance, false, false); |
| 13346 | |
| 13347 | if (methodMatches) |
| 13348 | { |
| 13349 | if (methodInstance->GetOwner()->IsFunction()) |
| 13350 | { |
| 13351 | BF_ASSERT(targetValue); |
| 13352 | return targetValue.mValue; |
| 13353 | } |
| 13354 | |
| 13355 | BfIRFunction bindFuncVal = irFunc; |
| 13356 | if (!bindFuncVal) |
| 13357 | { |
| 13358 | BfModuleMethodInstance methodRefMethod; |
| 13359 | if (methodInstance->mDeclModule == this) |
| 13360 | methodRefMethod = methodInstance; |
| 13361 | else |
| 13362 | methodRefMethod = ReferenceExternalMethodInstance(methodInstance); |
| 13363 | auto dataType = GetPrimitiveType(BfTypeCode_IntPtr); |
| 13364 | if (!methodRefMethod.mFunc) |
| 13365 | { |
| 13366 | if ((!methodInstance->mIsUnspecialized) && (HasCompiledOutput())) |
| 13367 | AssertErrorState(); |
| 13368 | return GetDefaultTypedValue(dataType, false, BfDefaultValueKind_Value).mValue; |
| 13369 | } |
| 13370 | bindFuncVal = methodRefMethod.mFunc; |
| 13371 | } |
| 13372 | if ((mCompiler->mOptions.mAllowHotSwapping) && (!mIsComptimeModule)) |
| 13373 | bindFuncVal = mBfIRBuilder->RemapBindFunction(bindFuncVal); |
| 13374 | return mBfIRBuilder->CreatePtrToInt(bindFuncVal, BfTypeCode_IntPtr); |
| 13375 | } |
| 13376 | |
| 13377 | if ((castFlags & BfCastFlags_SilentFail) == 0) |
| 13378 | { |
no test coverage detected