MCPcopy Create free account
hub / github.com/beefytech/Beef / GetTypeDistance

Method GetTypeDistance

IDEHelper/Compiler/BfModuleTypeUtils.cpp:15653–15718  ·  view source on GitHub ↗

Positive value means that toType encompasses fromType, negative value means toType is encompassed by formType INT_MAX means the types are not related

Source from the content-addressed store, hash-verified

15651// Positive value means that toType encompasses fromType, negative value means toType is encompassed by formType
15652// INT_MAX means the types are not related
15653int BfModule::GetTypeDistance(BfType* fromType, BfType* toType)
15654{
15655 if (fromType == toType)
15656 return 0;
15657
15658 if (fromType->IsPrimitiveType())
15659 {
15660 if (!toType->IsPrimitiveType())
15661 return INT_MAX;
15662 auto fromPrimType = (BfPrimitiveType*)fromType;
15663 auto toPrimType = (BfPrimitiveType*)toType;
15664
15665 if ((fromPrimType->IsIntegral()) && (toPrimType->IsIntegral()))
15666 {
15667 int fromBitSize = fromPrimType->mSize * 8;
15668 if (fromPrimType->IsSigned())
15669 fromBitSize--;
15670 int toBitSize = toPrimType->mSize * 8;
15671 if (toPrimType->IsSigned())
15672 toBitSize--;
15673 return fromBitSize - toBitSize;
15674 }
15675
15676 if ((fromPrimType->IsFloat()) && (toPrimType->IsFloat()))
15677 {
15678 return (fromPrimType->mSize * 8) - (toPrimType->mSize * 8);
15679 }
15680
15681 if (((fromPrimType->IsIntegral()) || (fromPrimType->IsFloat())) &&
15682 ((toPrimType->IsIntegral()) || (toPrimType->IsFloat())))
15683 {
15684 int sizeDiff = (fromPrimType->mSize * 8) - (toPrimType->mSize * 8);
15685 if (sizeDiff < 0)
15686 sizeDiff--;
15687 else
15688 sizeDiff++;
15689 return sizeDiff;
15690 }
15691 return INT_MAX;
15692 }
15693
15694 auto fromTypeInstance = fromType->ToTypeInstance();
15695 auto toTypeInstance = toType->ToTypeInstance();
15696
15697 if ((fromTypeInstance != NULL) != (toTypeInstance != NULL))
15698 return INT_MAX; // Ever valid?
15699
15700 if ((fromTypeInstance != NULL) && (toTypeInstance != NULL))
15701 {
15702 if ((fromTypeInstance->IsNullable()) && (toTypeInstance->IsNullable()))
15703 return GetTypeDistance(fromTypeInstance->GetUnderlyingType(), toTypeInstance->GetUnderlyingType());
15704
15705 int inheritDistance = toTypeInstance->mInheritDepth - fromTypeInstance->mInheritDepth;
15706 auto mostSpecificInstance = (inheritDistance < 0) ? fromTypeInstance : toTypeInstance;
15707 auto leastSpecificInstance = (inheritDistance < 0) ? toTypeInstance : fromTypeInstance;
15708
15709 while (mostSpecificInstance != NULL)
15710 {

Callers

nothing calls this directly

Calls 7

IsPrimitiveTypeMethod · 0.45
IsIntegralMethod · 0.45
IsSignedMethod · 0.45
IsFloatMethod · 0.45
ToTypeInstanceMethod · 0.45
IsNullableMethod · 0.45
GetUnderlyingTypeMethod · 0.45

Tested by

no test coverage detected