| 13252 | } |
| 13253 | |
| 13254 | BfType* BfModule::GetClosestNumericCastType(const BfTypedValue& typedVal, BfType* wantType) |
| 13255 | { |
| 13256 | BfType* toType = wantType; |
| 13257 | if ((toType == NULL) || |
| 13258 | ((!toType->IsFloat()) && (!toType->IsIntegral()))) |
| 13259 | toType = NULL; |
| 13260 | |
| 13261 | BfType* bestReturnType = NULL; |
| 13262 | |
| 13263 | if (typedVal.mType->IsTypedPrimitive()) |
| 13264 | return NULL; |
| 13265 | |
| 13266 | auto checkType = typedVal.mType->ToTypeInstance(); |
| 13267 | while (checkType != NULL) |
| 13268 | { |
| 13269 | for (auto operatorDef : checkType->mTypeDef->mOperators) |
| 13270 | { |
| 13271 | if (operatorDef->mOperatorDeclaration->mIsConvOperator) |
| 13272 | { |
| 13273 | if (operatorDef->IsExplicit()) |
| 13274 | continue; |
| 13275 | |
| 13276 | auto returnType = CheckOperator(checkType, operatorDef, typedVal, BfTypedValue()); |
| 13277 | if ((returnType != NULL) && |
| 13278 | ((returnType->IsIntegral()) || (returnType->IsFloat()))) |
| 13279 | { |
| 13280 | bool canCastTo = true; |
| 13281 | |
| 13282 | if ((toType != NULL) && (!CanCast(GetFakeTypedValue(returnType), toType))) |
| 13283 | canCastTo = false; |
| 13284 | |
| 13285 | if (canCastTo) |
| 13286 | { |
| 13287 | if (bestReturnType == NULL) |
| 13288 | { |
| 13289 | bestReturnType = returnType; |
| 13290 | } |
| 13291 | else |
| 13292 | { |
| 13293 | if (CanCast(GetFakeTypedValue(bestReturnType), returnType)) |
| 13294 | { |
| 13295 | bestReturnType = returnType; |
| 13296 | } |
| 13297 | } |
| 13298 | } |
| 13299 | } |
| 13300 | } |
| 13301 | } |
| 13302 | |
| 13303 | checkType = checkType->mBaseType; |
| 13304 | } |
| 13305 | |
| 13306 | if ((toType == NULL) && (bestReturnType != NULL)) |
| 13307 | { |
| 13308 | auto intPtrType = GetPrimitiveType(BfTypeCode_IntPtr); |
| 13309 | if (!CanCast(GetFakeTypedValue(bestReturnType), intPtrType)) |
| 13310 | { |
| 13311 | // If no 'wantType' is specified, try to get closest one to an intptr |
no test coverage detected