1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-09 03:52:19 +02:00

Fixed generic alias and generic delegate issues

This commit is contained in:
Brian Fiete 2020-05-23 17:25:47 -07:00
parent 639430b41c
commit 5a5287bc8b
10 changed files with 702 additions and 434 deletions

View file

@ -4881,7 +4881,8 @@ void BfCompiler::PopulateReified()
auto typeInst = type->ToTypeInstance(); auto typeInst = type->ToTypeInstance();
if ((typeInst != NULL) && (typeInst->IsGenericTypeInstance()) && (!typeInst->IsUnspecializedType())) if ((typeInst != NULL) && (typeInst->IsGenericTypeInstance()) && (!typeInst->IsUnspecializedType()) &&
(!typeInst->IsDelegateFromTypeRef()) && (!typeInst->IsFunctionFromTypeRef()))
{ {
auto unspecializedType = module->GetUnspecializedTypeInstance(typeInst); auto unspecializedType = module->GetUnspecializedTypeInstance(typeInst);
if (!unspecializedType->mIsReified) if (!unspecializedType->mIsReified)

View file

@ -758,6 +758,7 @@ BfType * BfContext::FindTypeById(int typeId)
void BfContext::AddTypeToWorkList(BfType* type) void BfContext::AddTypeToWorkList(BfType* type)
{ {
BF_ASSERT((type->mRebuildFlags & BfTypeRebuildFlag_InTempPool) == 0);
if ((type->mRebuildFlags & BfTypeRebuildFlag_AddedToWorkList) == 0) if ((type->mRebuildFlags & BfTypeRebuildFlag_AddedToWorkList) == 0)
{ {
type->mRebuildFlags = (BfTypeRebuildFlags)(type->mRebuildFlags | BfTypeRebuildFlag_AddedToWorkList); type->mRebuildFlags = (BfTypeRebuildFlags)(type->mRebuildFlags | BfTypeRebuildFlag_AddedToWorkList);
@ -2221,9 +2222,10 @@ void BfContext::GenerateModuleName_Type(BfType* type, String& name)
if (type->IsDelegateFromTypeRef() || type->IsFunctionFromTypeRef()) if (type->IsDelegateFromTypeRef() || type->IsFunctionFromTypeRef())
{ {
auto delegateType = (BfDelegateType*)type; auto typeInst = type->ToTypeInstance();
auto delegateInfo = type->GetDelegateInfo();
auto methodDef = delegateType->mTypeDef->mMethods[0]; auto methodDef = typeInst->mTypeDef->mMethods[0];
if (type->IsDelegateFromTypeRef()) if (type->IsDelegateFromTypeRef())
name += "DELEGATE_"; name += "DELEGATE_";

View file

@ -382,6 +382,7 @@ public:
BfAllocPool<BfConcreteInterfaceType> mConcreteInterfaceTypePool; BfAllocPool<BfConcreteInterfaceType> mConcreteInterfaceTypePool;
BfAllocPool<BfConstExprValueType> mConstExprValueTypePool; BfAllocPool<BfConstExprValueType> mConstExprValueTypePool;
BfAllocPool<BfDelegateType> mDelegateTypePool; BfAllocPool<BfDelegateType> mDelegateTypePool;
BfAllocPool<BfGenericDelegateType> mGenericDelegateTypePool;
BfPrimitiveType* mPrimitiveTypes[BfTypeCode_Length]; BfPrimitiveType* mPrimitiveTypes[BfTypeCode_Length];
BfPrimitiveType* mPrimitiveStructTypes[BfTypeCode_Length]; BfPrimitiveType* mPrimitiveStructTypes[BfTypeCode_Length];

View file

@ -562,9 +562,9 @@ void BfMethodMatcher::CompareMethods(BfMethodInstance* prevMethodInstance, BfTyp
BfType* origPrevParamType = prevParamType; BfType* origPrevParamType = prevParamType;
if ((genericArgumentsSubstitute != NULL) && (paramType->IsUnspecializedType())) if ((genericArgumentsSubstitute != NULL) && (paramType->IsUnspecializedType()))
paramType = mModule->ResolveGenericType(paramType, *genericArgumentsSubstitute, allowSpecializeFail); paramType = mModule->ResolveGenericType(paramType, NULL, genericArgumentsSubstitute, allowSpecializeFail);
if ((prevGenericArgumentsSubstitute != NULL) && (prevParamType->IsUnspecializedType())) if ((prevGenericArgumentsSubstitute != NULL) && (prevParamType->IsUnspecializedType()))
prevParamType = mModule->ResolveGenericType(prevParamType, *prevGenericArgumentsSubstitute, allowSpecializeFail); prevParamType = mModule->ResolveGenericType(prevParamType, NULL, prevGenericArgumentsSubstitute, allowSpecializeFail);
if ((wasGenericParam) || (prevWasGenericParam)) if ((wasGenericParam) || (prevWasGenericParam))
{ {
@ -957,7 +957,7 @@ BfTypedValue BfMethodMatcher::ResolveArgTypedValue(BfResolvedArg& resolvedArg, B
} }
if ((genericArgumentsSubstitute != NULL) && (expectType->IsUnspecializedType())) if ((genericArgumentsSubstitute != NULL) && (expectType->IsUnspecializedType()))
expectType = mModule->ResolveGenericType(expectType, *genericArgumentsSubstitute, true); expectType = mModule->ResolveGenericType(expectType, NULL, genericArgumentsSubstitute, true);
} }
exprEvaluator.mExpectingType = expectType; exprEvaluator.mExpectingType = expectType;
@ -1067,13 +1067,13 @@ bool BfMethodMatcher::InferFromGenericConstraints(BfGenericParamInstance* generi
{ {
auto leftType = checkOpConstraint.mLeftType; auto leftType = checkOpConstraint.mLeftType;
if ((leftType != NULL) && (leftType->IsUnspecializedType())) if ((leftType != NULL) && (leftType->IsUnspecializedType()))
leftType = mModule->ResolveGenericType(leftType, *methodGenericArgs); leftType = mModule->ResolveGenericType(leftType, NULL, methodGenericArgs);
if (leftType != NULL) if (leftType != NULL)
leftType = mModule->FixIntUnknown(leftType); leftType = mModule->FixIntUnknown(leftType);
auto rightType = checkOpConstraint.mRightType; auto rightType = checkOpConstraint.mRightType;
if ((rightType != NULL) && (rightType->IsUnspecializedType())) if ((rightType != NULL) && (rightType->IsUnspecializedType()))
rightType = mModule->ResolveGenericType(rightType, *methodGenericArgs); rightType = mModule->ResolveGenericType(rightType, NULL, methodGenericArgs);
if (rightType != NULL) if (rightType != NULL)
rightType = mModule->FixIntUnknown(rightType); rightType = mModule->FixIntUnknown(rightType);
@ -1309,7 +1309,7 @@ bool BfMethodMatcher::CheckMethod(BfTypeInstance* targetTypeInstance, BfTypeInst
if ((checkType != NULL) && (genericArgumentsSubstitute != NULL) && (checkType->IsUnspecializedType())) if ((checkType != NULL) && (genericArgumentsSubstitute != NULL) && (checkType->IsUnspecializedType()))
{ {
checkType = mModule->ResolveGenericType(origCheckType, *genericArgumentsSubstitute); checkType = mModule->ResolveGenericType(origCheckType, NULL, genericArgumentsSubstitute);
} }
if (wantType->IsUnspecializedType()) if (wantType->IsUnspecializedType())
@ -1453,7 +1453,7 @@ bool BfMethodMatcher::CheckMethod(BfTypeInstance* targetTypeInstance, BfTypeInst
auto wantType = methodInstance->GetParamType(paramIdx); auto wantType = methodInstance->GetParamType(paramIdx);
if ((genericArgumentsSubstitute != NULL) && (wantType->IsUnspecializedType())) if ((genericArgumentsSubstitute != NULL) && (wantType->IsUnspecializedType()))
{ {
auto resolvedType = mModule->ResolveGenericType(wantType, *genericArgumentsSubstitute); auto resolvedType = mModule->ResolveGenericType(wantType, NULL, genericArgumentsSubstitute);
if (resolvedType == NULL) if (resolvedType == NULL)
goto NoMatch; goto NoMatch;
wantType = resolvedType; wantType = resolvedType;
@ -6306,7 +6306,7 @@ BfTypedValue BfExprEvaluator::MatchMethod(BfAstNode* targetSrc, BfMethodBoundExp
if (isUnboundCall) if (isUnboundCall)
{ {
if (mModule->mCurMethodInstance->mIsUnspecialized) if ((mModule->mCurMethodInstance != NULL) && (mModule->mCurMethodInstance->mIsUnspecialized))
{ {
auto varType = mModule->GetPrimitiveType(BfTypeCode_Var); auto varType = mModule->GetPrimitiveType(BfTypeCode_Var);

View file

@ -6861,7 +6861,7 @@ bool BfModule::CheckGenericConstraints(const BfGenericParamSource& genericParamS
{ {
BfType* convCheckConstraint = genericParamInst->mTypeConstraint; BfType* convCheckConstraint = genericParamInst->mTypeConstraint;
if ((convCheckConstraint->IsUnspecializedType()) && (methodGenericArgs != NULL)) if ((convCheckConstraint->IsUnspecializedType()) && (methodGenericArgs != NULL))
convCheckConstraint = ResolveGenericType(convCheckConstraint, *methodGenericArgs); convCheckConstraint = ResolveGenericType(convCheckConstraint, NULL, methodGenericArgs);
if ((checkArgType->IsMethodRef()) && (convCheckConstraint->IsDelegate())) if ((checkArgType->IsMethodRef()) && (convCheckConstraint->IsDelegate()))
{ {
auto methodRefType = (BfMethodRefType*)checkArgType; auto methodRefType = (BfMethodRefType*)checkArgType;
@ -6932,7 +6932,7 @@ bool BfModule::CheckGenericConstraints(const BfGenericParamSource& genericParamS
{ {
BfType* convCheckConstraint = checkConstraint; BfType* convCheckConstraint = checkConstraint;
if (convCheckConstraint->IsUnspecializedType()) if (convCheckConstraint->IsUnspecializedType())
convCheckConstraint = ResolveGenericType(convCheckConstraint, *methodGenericArgs); convCheckConstraint = ResolveGenericType(convCheckConstraint, NULL, methodGenericArgs);
BfTypeInstance* typeConstraintInst = convCheckConstraint->ToTypeInstance(); BfTypeInstance* typeConstraintInst = convCheckConstraint->ToTypeInstance();
@ -6973,13 +6973,13 @@ bool BfModule::CheckGenericConstraints(const BfGenericParamSource& genericParamS
{ {
auto leftType = checkOpConstraint.mLeftType; auto leftType = checkOpConstraint.mLeftType;
if ((leftType != NULL) && (leftType->IsUnspecializedType())) if ((leftType != NULL) && (leftType->IsUnspecializedType()))
leftType = ResolveGenericType(leftType, *methodGenericArgs); leftType = ResolveGenericType(leftType, NULL, methodGenericArgs);
if (leftType != NULL) if (leftType != NULL)
leftType = FixIntUnknown(leftType); leftType = FixIntUnknown(leftType);
auto rightType = checkOpConstraint.mRightType; auto rightType = checkOpConstraint.mRightType;
if ((rightType != NULL) && (rightType->IsUnspecializedType())) if ((rightType != NULL) && (rightType->IsUnspecializedType()))
rightType = ResolveGenericType(rightType, *methodGenericArgs); rightType = ResolveGenericType(rightType, NULL, methodGenericArgs);
if (rightType != NULL) if (rightType != NULL)
rightType = FixIntUnknown(rightType); rightType = FixIntUnknown(rightType);
@ -8793,15 +8793,26 @@ BfTypedValue BfModule::BoxValue(BfAstNode* srcNode, BfTypedValue typedVal, BfTyp
if (fromStructTypeInstance == NULL) if (fromStructTypeInstance == NULL)
{ {
auto primType = (BfPrimitiveType*)typedVal.mType; auto primType = (BfPrimitiveType*)typedVal.mType;
if ((typedVal.mType->IsPointer()) && (toTypeInstance->IsInstanceOf(mCompiler->mIHashableTypeDef)))
{
// Can always do IHashable
alreadyCheckedCast = true;
}
if ((!typedVal.mType->IsPointer()) || (toTypeInstance == mContext->mBfObjectType))
fromStructTypeInstance = GetWrappedStructType(typedVal.mType); fromStructTypeInstance = GetWrappedStructType(typedVal.mType);
if (isStructPtr) if (isStructPtr)
{ {
if ((toTypeInstance != NULL) && (TypeIsSubTypeOf(fromStructTypeInstance, toTypeInstance))) if ((toTypeInstance != NULL) && (fromStructTypeInstance != NULL) && (TypeIsSubTypeOf(fromStructTypeInstance, toTypeInstance)))
alreadyCheckedCast = true; alreadyCheckedCast = true;
fromStructTypeInstance = typedVal.mType->GetUnderlyingType()->ToTypeInstance(); fromStructTypeInstance = typedVal.mType->GetUnderlyingType()->ToTypeInstance();
} }
if ((fromStructTypeInstance == NULL) && (alreadyCheckedCast))
fromStructTypeInstance = GetWrappedStructType(typedVal.mType);
} }
if (fromStructTypeInstance == NULL) if (fromStructTypeInstance == NULL)
return BfTypedValue(); return BfTypedValue();
@ -9307,7 +9318,7 @@ String BfModule::MethodToString(BfMethodInstance* methodInst, BfMethodNameFlags
BfType* type = methodInst->mMethodInstanceGroup->mOwner; BfType* type = methodInst->mMethodInstanceGroup->mOwner;
if ((methodGenericArgs != NULL) && (type->IsUnspecializedType())) if ((methodGenericArgs != NULL) && (type->IsUnspecializedType()))
type = ResolveGenericType(type, *methodGenericArgs); type = ResolveGenericType(type, NULL, methodGenericArgs);
String methodName; String methodName;
if ((methodNameFlags & BfMethodNameFlag_OmitTypeName) == 0) if ((methodNameFlags & BfMethodNameFlag_OmitTypeName) == 0)
{ {
@ -9453,7 +9464,7 @@ String BfModule::MethodToString(BfMethodInstance* methodInst, BfMethodNameFlags
} }
if (type->IsUnspecializedType()) if (type->IsUnspecializedType())
type = ResolveGenericType(type, *methodGenericArgs); type = ResolveGenericType(type, NULL, methodGenericArgs);
} }
if ((methodGenericArgs == NULL) && (mCurMethodInstance == NULL) && (mCurTypeInstance == NULL)) if ((methodGenericArgs == NULL) && (mCurMethodInstance == NULL) && (mCurTypeInstance == NULL))
@ -9486,7 +9497,7 @@ String BfModule::MethodToString(BfMethodInstance* methodInst, BfMethodNameFlags
typeNameFlags = BfTypeNameFlag_ResolveGenericParamNames; typeNameFlags = BfTypeNameFlag_ResolveGenericParamNames;
BfType* type = methodInst->GetParamType(paramIdx); BfType* type = methodInst->GetParamType(paramIdx);
if ((methodGenericArgs != NULL) && (type->IsUnspecializedType())) if ((methodGenericArgs != NULL) && (type->IsUnspecializedType()))
type = ResolveGenericType(type, *methodGenericArgs); type = ResolveGenericType(type, NULL, methodGenericArgs);
methodName += TypeToString(type, typeNameFlags); methodName += TypeToString(type, typeNameFlags);
methodName += " "; methodName += " ";

View file

@ -81,7 +81,8 @@ enum BfCastFlags
BfCastFlags_FromCompiler = 0x40, // Not user specified BfCastFlags_FromCompiler = 0x40, // Not user specified
BfCastFlags_Force = 0x80, BfCastFlags_Force = 0x80,
BfCastFlags_PreferAddr = 0x100, BfCastFlags_PreferAddr = 0x100,
BfCastFlags_WarnOnBox = 0x200 BfCastFlags_WarnOnBox = 0x200,
BfCastFlags_IsCastCheck = 0x400
}; };
enum BfCastResultFlags enum BfCastResultFlags
@ -1646,7 +1647,7 @@ public:
BfType* FixIntUnknown(BfType* type); BfType* FixIntUnknown(BfType* type);
void FixIntUnknown(BfTypedValue& typedVal); void FixIntUnknown(BfTypedValue& typedVal);
void FixIntUnknown(BfTypedValue& lhs, BfTypedValue& rhs); void FixIntUnknown(BfTypedValue& lhs, BfTypedValue& rhs);
BfTypeDef* ResolveGenericInstanceDef(BfGenericInstanceTypeRef* genericTypeRef); BfTypeDef* ResolveGenericInstanceDef(BfGenericInstanceTypeRef* genericTypeRef, BfType** outType = NULL);
BfType* ResolveType(BfType* lookupType, BfPopulateType populateType = BfPopulateType_Data); BfType* ResolveType(BfType* lookupType, BfPopulateType populateType = BfPopulateType_Data);
void ResolveGenericParamConstraints(BfGenericParamInstance* genericParamInstance, bool isUnspecialized); void ResolveGenericParamConstraints(BfGenericParamInstance* genericParamInstance, bool isUnspecialized);
String GenericParamSourceToString(const BfGenericParamSource& genericParamSource); String GenericParamSourceToString(const BfGenericParamSource& genericParamSource);
@ -1677,7 +1678,7 @@ public:
void EmitDeferredScopeCalls(bool useSrcPositions, BfScopeData* scope, BfIRBlock doneBlock = BfIRBlock()); void EmitDeferredScopeCalls(bool useSrcPositions, BfScopeData* scope, BfIRBlock doneBlock = BfIRBlock());
void MarkScopeLeft(BfScopeData* scopeData); void MarkScopeLeft(BfScopeData* scopeData);
BfGenericParamType* GetGenericParamType(BfGenericParamKind paramKind, int paramIdx); BfGenericParamType* GetGenericParamType(BfGenericParamKind paramKind, int paramIdx);
BfType* ResolveGenericType(BfType* unspecializedType, const BfTypeVector& methodGenericArguments, bool allowFail = false); BfType* ResolveGenericType(BfType* unspecializedType, BfTypeVector* typeGenericArguments, BfTypeVector* methodGenericArguments, bool allowFail = false);
bool IsUnboundGeneric(BfType* type); bool IsUnboundGeneric(BfType* type);
BfGenericParamInstance* GetGenericTypeParamInstance(int paramIdx); BfGenericParamInstance* GetGenericTypeParamInstance(int paramIdx);
BfGenericParamInstance* GetGenericParamInstance(BfGenericParamType* type); BfGenericParamInstance* GetGenericParamInstance(BfGenericParamType* type);

File diff suppressed because it is too large Load diff

View file

@ -1908,8 +1908,6 @@ void BfClosureType::Finish()
BfDelegateType::~BfDelegateType() BfDelegateType::~BfDelegateType()
{ {
delete mTypeDef; delete mTypeDef;
for (auto directAllocNode : mDirectAllocNodes)
delete directAllocNode;
} }
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
@ -2129,6 +2127,19 @@ int BfResolvedTypeSet::Hash(BfType* type, LookupContext* ctx, bool allowRef)
{ {
//BP_ZONE("BfResolvedTypeSet::Hash"); //BP_ZONE("BfResolvedTypeSet::Hash");
// if (type->IsTypeAlias())
// {
// auto underlyingType = type->GetUnderlyingType();
// BF_ASSERT(underlyingType != NULL);
// if (underlyingType == NULL)
// {
// ctx->mFailed = true;
// return 0;
// }
// return Hash(underlyingType, ctx, allowRef);
// }
// else
if (type->IsBoxed()) if (type->IsBoxed())
{ {
BfBoxedType* boxedType = (BfBoxedType*)type; BfBoxedType* boxedType = (BfBoxedType*)type;
@ -2143,20 +2154,21 @@ int BfResolvedTypeSet::Hash(BfType* type, LookupContext* ctx, bool allowRef)
} }
else if (type->IsDelegateFromTypeRef() || type->IsFunctionFromTypeRef()) else if (type->IsDelegateFromTypeRef() || type->IsFunctionFromTypeRef())
{ {
auto delegateType = (BfDelegateType*)type; auto typeInst = (BfTypeInstance*)type;
int hashVal = HASH_DELEGATE; int hashVal = HASH_DELEGATE;
hashVal = ((hashVal ^ (Hash(delegateType->mReturnType, ctx))) << 5) - hashVal; auto delegateInfo = type->GetDelegateInfo();
auto methodDef = delegateType->mTypeDef->mMethods[0]; hashVal = ((hashVal ^ (Hash(delegateInfo->mReturnType, ctx))) << 5) - hashVal;
auto methodDef = typeInst->mTypeDef->mMethods[0];
BF_ASSERT(methodDef->mName == "Invoke"); BF_ASSERT(methodDef->mName == "Invoke");
BF_ASSERT(delegateType->mParams.size() == methodDef->mParams.size()); BF_ASSERT(delegateInfo->mParams.size() == methodDef->mParams.size());
for (int paramIdx = 0; paramIdx < delegateType->mParams.size(); paramIdx++) for (int paramIdx = 0; paramIdx < delegateInfo->mParams.size(); paramIdx++)
{ {
// Parse attributes? // Parse attributes?
hashVal = ((hashVal ^ (Hash(delegateType->mParams[paramIdx], ctx))) << 5) - hashVal; hashVal = ((hashVal ^ (Hash(delegateInfo->mParams[paramIdx], ctx))) << 5) - hashVal;
String paramName = methodDef->mParams[paramIdx]->mName; String paramName = methodDef->mParams[paramIdx]->mName;
int nameHash = (int)Hash64(paramName.c_str(), (int)paramName.length()); int nameHash = (int)Hash64(paramName.c_str(), (int)paramName.length());
hashVal = ((hashVal ^ (nameHash)) << 5) - hashVal; hashVal = ((hashVal ^ (nameHash)) << 5) - hashVal;
@ -2313,60 +2325,27 @@ static int HashNode(BfAstNode* node)
return (int)Hash64(nameStr, node->GetSrcLength()); return (int)Hash64(nameStr, node->GetSrcLength());
} }
int BfResolvedTypeSet::DirectHash(BfTypeReference* typeRef, LookupContext* ctx, BfHashFlags flags)
{
bool isHeadType = typeRef == ctx->mRootTypeRef;
BfResolveTypeRefFlags resolveFlags = BfResolveTypeRefFlag_None;
if ((flags & BfHashFlag_AllowGenericParamConstValue) != 0)
resolveFlags = (BfResolveTypeRefFlags)(resolveFlags | BfResolveTypeRefFlag_AllowGenericParamConstValue);
auto resolvedType = ctx->mModule->ResolveTypeRef(typeRef, BfPopulateType_Identity, resolveFlags);
if (resolvedType == NULL)
{
ctx->mFailed = true;
return 0;
}
return Hash(resolvedType, ctx);
}
int BfResolvedTypeSet::Hash(BfTypeReference* typeRef, LookupContext* ctx, BfHashFlags flags) int BfResolvedTypeSet::Hash(BfTypeReference* typeRef, LookupContext* ctx, BfHashFlags flags)
{ {
// if (auto typeDefTypeRef = BfNodeDynCast<BfTypeDefTypeReference>(typeRef)) if ((typeRef == ctx->mRootTypeRef) && (ctx->mRootTypeDef != NULL) &&
// { ((typeRef->IsNamedTypeReference()) || (BfNodeIsA<BfDirectTypeDefReference>(typeRef))))
// if (typeDefTypeRef->mTypeDef != NULL)
// {
// int hashVal = typeDefTypeRef->mTypeDef->mHash;
//
// if (typeDefTypeRef->mTypeDef->mGenericParamDefs.size() != 0)
// {
// auto checkTypeInstance = ctx->mModule->mCurTypeInstance;
// if (checkTypeInstance->IsBoxed())
// checkTypeInstance = checkTypeInstance->GetUnderlyingType()->ToTypeInstance();
//
// //if (!module->TypeHasParent(module->mCurTypeInstance->mTypeDef, typeDefTypeRef->mTypeDef->mParentType))
// auto outerType = ctx->mModule->mSystem->GetOuterTypeNonPartial(typeDefTypeRef->mTypeDef);
// BfTypeDef* commonOuterType = ctx->mModule->FindCommonOuterType(ctx->mModule->mCurTypeInstance->mTypeDef, outerType);
// //BfTypeDef* commonOuterType = ctx->mModule->FindCommonOuterType(ctx->mModule->GetActiveTypeDef(), typeDefTypeRef->mTypeDef->mOuterType);
// if ((commonOuterType == NULL) || (commonOuterType->mGenericParamDefs.size() == 0))
// {
// ctx->mModule->Fail("Generic arguments expected", typeDefTypeRef);
// ctx->mFailed = true;
// return 0;
// }
//
// BF_ASSERT(checkTypeInstance->IsGenericTypeInstance());
// auto curGenericTypeInst = (BfGenericTypeInstance*)checkTypeInstance;
// //int numParentGenericParams = (int)typeDefTypeRef->mTypeDef->mGenericParams.size();
// int numParentGenericParams = (int)commonOuterType->mGenericParamDefs.size();
// for (int i = 0; i < numParentGenericParams; i++)
// {
// hashVal = ((hashVal ^ (Hash(curGenericTypeInst->mTypeGenericArguments[i], ctx))) << 5) - hashVal;
// }
// }
//
// return hashVal;
// }
//
// bool isHeadType = typeRef == ctx->mRootTypeRef;
//
// BfResolveTypeRefFlags resolveFlags = BfResolveTypeRefFlag_None;
// if ((flags & BfHashFlag_AllowGenericParamConstValue) != 0)
// resolveFlags = (BfResolveTypeRefFlags)(resolveFlags | BfResolveTypeRefFlag_AllowGenericParamConstValue);
//
// auto resolvedType = ctx->mModule->ResolveTypeRef(typeRef, BfPopulateType_Identity, resolveFlags);
// if (resolvedType == NULL)
// {
// ctx->mFailed = true;
// return 0;
// }
// return Hash(resolvedType, ctx);
// }
if ((typeRef == ctx->mRootTypeRef) && (ctx->mRootTypeDef != NULL))
{ {
BfTypeDef* typeDef = ctx->mRootTypeDef; BfTypeDef* typeDef = ctx->mRootTypeDef;
@ -2401,24 +2380,48 @@ int BfResolvedTypeSet::Hash(BfTypeReference* typeRef, LookupContext* ctx, BfHash
if (typeRef->IsNamedTypeReference()) if (typeRef->IsNamedTypeReference())
{ {
bool isHeadType = typeRef == ctx->mRootTypeRef; return DirectHash(typeRef, ctx, flags);
BfResolveTypeRefFlags resolveFlags = BfResolveTypeRefFlag_None;
if ((flags & BfHashFlag_AllowGenericParamConstValue) != 0)
resolveFlags = (BfResolveTypeRefFlags)(resolveFlags | BfResolveTypeRefFlag_AllowGenericParamConstValue);
auto resolvedType = ctx->mModule->ResolveTypeRef(typeRef, BfPopulateType_Identity, resolveFlags);
if (resolvedType == NULL)
{
ctx->mFailed = true;
return 0;
}
return Hash(resolvedType, ctx);
} }
else if (auto genericInstTypeRef = BfNodeDynCastExact<BfGenericInstanceTypeRef>(typeRef)) else if (auto genericInstTypeRef = BfNodeDynCastExact<BfGenericInstanceTypeRef>(typeRef))
{ {
//BfType* type = NULL; //BfType* type = NULL;
BfTypeDef* elementTypeDef = ctx->mModule->ResolveGenericInstanceDef(genericInstTypeRef); BfTypeDef* elementTypeDef = ctx->mModule->ResolveGenericInstanceDef(genericInstTypeRef);
if (elementTypeDef == NULL)
{
ctx->mFailed = true;
return 0;
}
// Don't translate aliases for the root type, just element types
if (ctx->mRootTypeRef == typeRef)
{
BF_ASSERT((ctx->mRootTypeDef == NULL) || (ctx->mRootTypeDef == elementTypeDef));
ctx->mRootTypeDef = elementTypeDef;
}
else if (elementTypeDef->mTypeCode == BfTypeCode_TypeAlias)
{
BfTypeVector genericArgs;
for (auto genericArgTypeRef : genericInstTypeRef->mGenericArguments)
{
auto argType = ctx->mModule->ResolveTypeRef(genericArgTypeRef, BfPopulateType_Identity);
if (argType != NULL)
genericArgs.Add(argType);
else
ctx->mFailed = true;
}
if (!ctx->mFailed)
{
auto resolvedType = ctx->mModule->ResolveTypeDef(elementTypeDef, genericArgs);
if ((resolvedType != NULL) && (resolvedType->IsTypeAlias()))
{
auto underlyingType = resolvedType->GetUnderlyingType();
return Hash(underlyingType, ctx, flags);
}
}
}
int hashVal; int hashVal;
/*if (type != NULL) /*if (type != NULL)
{ {
@ -2426,17 +2429,7 @@ int BfResolvedTypeSet::Hash(BfTypeReference* typeRef, LookupContext* ctx, BfHash
} }
else */ else */
{ {
if (elementTypeDef == NULL)
{
ctx->mFailed = true;
return 0;
}
if (ctx->mRootTypeRef == typeRef)
{
BF_ASSERT((ctx->mRootTypeDef == NULL) || (ctx->mRootTypeDef == elementTypeDef));
ctx->mRootTypeDef = elementTypeDef;
}
hashVal = elementTypeDef->mHash; hashVal = elementTypeDef->mHash;
} }
@ -2811,21 +2804,21 @@ bool BfResolvedTypeSet::Equals(BfType* lhs, BfType* rhs, LookupContext* ctx)
return false; return false;
if (lhs->IsDelegate() != rhs->IsDelegate()) if (lhs->IsDelegate() != rhs->IsDelegate())
return false; return false;
BfDelegateType* lhsDelegateType = (BfDelegateType*)lhs; BfDelegateInfo* lhsDelegateInfo = lhs->GetDelegateInfo();
BfDelegateType* rhsDelegateType = (BfDelegateType*)rhs; BfDelegateInfo* rhsDelegateInfo = rhs->GetDelegateInfo();
if (lhsDelegateType->mTypeDef->mIsDelegate != rhsDelegateType->mTypeDef->mIsDelegate) if (lhsInst->mTypeDef->mIsDelegate != rhsInst->mTypeDef->mIsDelegate)
return false; return false;
auto lhsMethodDef = lhsDelegateType->mTypeDef->mMethods[0]; auto lhsMethodDef = lhsInst->mTypeDef->mMethods[0];
auto rhsMethodDef = rhsDelegateType->mTypeDef->mMethods[0]; auto rhsMethodDef = rhsInst->mTypeDef->mMethods[0];
if (lhsDelegateType->mReturnType != rhsDelegateType->mReturnType) if (lhsDelegateInfo->mReturnType != rhsDelegateInfo->mReturnType)
return false; return false;
if (lhsDelegateType->mParams.size() != rhsDelegateType->mParams.size()) if (lhsDelegateInfo->mParams.size() != rhsDelegateInfo->mParams.size())
return false; return false;
for (int paramIdx = 0; paramIdx < lhsDelegateType->mParams.size(); paramIdx++) for (int paramIdx = 0; paramIdx < lhsDelegateInfo->mParams.size(); paramIdx++)
{ {
if (lhsDelegateType->mParams[paramIdx] != rhsDelegateType->mParams[paramIdx]) if (lhsDelegateInfo->mParams[paramIdx] != rhsDelegateInfo->mParams[paramIdx])
return false; return false;
if (lhsMethodDef->mParams[paramIdx]->mName != rhsMethodDef->mParams[paramIdx]->mName) if (lhsMethodDef->mParams[paramIdx]->mName != rhsMethodDef->mParams[paramIdx]->mName)
return false; return false;
@ -3104,8 +3097,11 @@ BfType* BfResolvedTypeSet::LookupContext::ResolveTypeRef(BfTypeReference* typeRe
return mModule->ResolveTypeRef(typeReference, BfPopulateType_Identity, BfResolveTypeRefFlag_AllowGenericParamConstValue); return mModule->ResolveTypeRef(typeReference, BfPopulateType_Identity, BfResolveTypeRefFlag_AllowGenericParamConstValue);
} }
BfTypeDef* BfResolvedTypeSet::LookupContext::ResolveToTypeDef(BfTypeReference* typeReference) BfTypeDef* BfResolvedTypeSet::LookupContext::ResolveToTypeDef(BfTypeReference* typeReference, BfType** outType)
{ {
if (outType != NULL)
*outType = NULL;
if (typeReference == mRootTypeRef) if (typeReference == mRootTypeRef)
return mRootTypeDef; return mRootTypeDef;
@ -3117,6 +3113,8 @@ BfTypeDef* BfResolvedTypeSet::LookupContext::ResolveToTypeDef(BfTypeReference* t
auto type = mModule->ResolveTypeRef(typeReference, BfPopulateType_Identity, BfResolveTypeRefFlag_AllowGenericParamConstValue); auto type = mModule->ResolveTypeRef(typeReference, BfPopulateType_Identity, BfResolveTypeRefFlag_AllowGenericParamConstValue);
if (type == NULL) if (type == NULL)
return NULL; return NULL;
if (outType != NULL)
*outType = type;
if (type->IsPrimitiveType()) if (type->IsPrimitiveType())
return ((BfPrimitiveType*)type)->mTypeDef; return ((BfPrimitiveType*)type)->mTypeDef;
auto typeInst = type->ToTypeInstance(); auto typeInst = type->ToTypeInstance();
@ -3125,10 +3123,27 @@ BfTypeDef* BfResolvedTypeSet::LookupContext::ResolveToTypeDef(BfTypeReference* t
return typeInst->mTypeDef; return typeInst->mTypeDef;
} }
bool BfResolvedTypeSet::Equals(BfType* lhs, BfTypeReference* rhs, LookupContext* ctx) bool BfResolvedTypeSet::Equals(BfType* lhs, BfTypeReference* rhs, BfTypeDef* rhsTypeDef, LookupContext* ctx)
{
auto rhsType = ctx->mModule->ResolveTypeDef(rhsTypeDef, BfPopulateType_Identity);
if (rhsType == NULL)
{
ctx->mFailed = true;
return false;
}
return BfResolvedTypeSet::Equals(lhs, rhsType, ctx);
}
bool BfResolvedTypeSet::EqualsNoAlias(BfType* lhs, BfTypeReference* rhs, LookupContext* ctx)
{ {
//BP_ZONE("BfResolvedTypeSet::Equals"); //BP_ZONE("BfResolvedTypeSet::Equals");
// if ((rhs == ctx->mRootTypeRef) && (ctx->mRootTypeDef != NULL) && (ctx->mRootTypeDef->mTypeCode == BfTypeCode_TypeAlias))
// {
// return lhs == ctx->mResolvedType;
// }
if (ctx->mRootTypeRef != rhs) if (ctx->mRootTypeRef != rhs)
{ {
if (auto retTypeRef = BfNodeDynCastExact<BfModifiedTypeRef>(rhs)) if (auto retTypeRef = BfNodeDynCastExact<BfModifiedTypeRef>(rhs))
@ -3198,9 +3213,9 @@ bool BfResolvedTypeSet::Equals(BfType* lhs, BfTypeReference* rhs, LookupContext*
auto rhsDelegateType = BfNodeDynCastExact<BfDelegateTypeRef>(rhs); auto rhsDelegateType = BfNodeDynCastExact<BfDelegateTypeRef>(rhs);
if (rhsDelegateType == NULL) if (rhsDelegateType == NULL)
return false; return false;
BfDelegateType* lhsDelegateType = (BfDelegateType*)lhs; BfDelegateInfo* lhsDelegateType = lhs->GetDelegateInfo();
BfMethodInstance* lhsInvokeMethodInstance = ctx->mModule->GetRawMethodInstanceAtIdx(lhsDelegateType->ToTypeInstance(), 0, "Invoke"); BfMethodInstance* lhsInvokeMethodInstance = ctx->mModule->GetRawMethodInstanceAtIdx(lhs->ToTypeInstance(), 0, "Invoke");
if ((lhs->IsDelegate()) != (rhsDelegateType->mTypeToken->GetToken() == BfToken_Delegate)) if ((lhs->IsDelegate()) != (rhsDelegateType->mTypeToken->GetToken() == BfToken_Delegate))
return false; return false;
@ -3227,10 +3242,14 @@ bool BfResolvedTypeSet::Equals(BfType* lhs, BfTypeReference* rhs, LookupContext*
if (lhs->IsGenericTypeInstance()) if (lhs->IsGenericTypeInstance())
{ {
auto rhsTypeDef = ctx->ResolveToTypeDef(rhs); BfType* rhsType = NULL;
auto rhsTypeDef = ctx->ResolveToTypeDef(rhs, &rhsType);
if (rhsTypeDef == NULL) if (rhsTypeDef == NULL)
return false; return false;
if (rhsType != NULL)
return lhs == rhsType;
BfGenericTypeInstance* lhsGenericType = (BfGenericTypeInstance*) lhs; BfGenericTypeInstance* lhsGenericType = (BfGenericTypeInstance*) lhs;
return GenericTypeEquals(lhsGenericType, &lhsGenericType->mTypeGenericArguments, rhs, rhsTypeDef, ctx); return GenericTypeEquals(lhsGenericType, &lhsGenericType->mTypeGenericArguments, rhs, rhsTypeDef, ctx);
} }
@ -3329,6 +3348,8 @@ bool BfResolvedTypeSet::Equals(BfType* lhs, BfTypeReference* rhs, LookupContext*
BfPrimitiveType* lhsPrimType = (BfPrimitiveType*)lhs; BfPrimitiveType* lhsPrimType = (BfPrimitiveType*)lhs;
auto rhsTypeDef = ctx->ResolveToTypeDef(rhs); auto rhsTypeDef = ctx->ResolveToTypeDef(rhs);
// if (rhsTypeDef->mTypeCode == BfTypeCode_TypeAlias)
// return Equals(lhs, rhs, rhsTypeDef, ctx);
return lhsPrimType->mTypeDef == rhsTypeDef; return lhsPrimType->mTypeDef == rhsTypeDef;
} }
else if (lhs->IsPointer()) else if (lhs->IsPointer())
@ -3483,6 +3504,22 @@ bool BfResolvedTypeSet::Equals(BfType* lhs, BfTypeReference* rhs, LookupContext*
return false; return false;
} }
bool BfResolvedTypeSet::Equals(BfType* lhs, BfTypeReference* rhs, LookupContext* ctx)
{
if (EqualsNoAlias(lhs, rhs, ctx))
return true;
// if (lhs->IsTypeAlias())
// {
// auto underylingType = lhs->GetUnderlyingType();
// BF_ASSERT(underylingType != NULL);
// if (underylingType != NULL)
// return Equals(underylingType, rhs, ctx);
// }
return false;
}
void BfResolvedTypeSet::RemoveEntry(BfResolvedTypeSet::Entry* entry) void BfResolvedTypeSet::RemoveEntry(BfResolvedTypeSet::Entry* entry)
{ {
int hashIdx = (entry->mHash & 0x7FFFFFFF) % mHashSize; int hashIdx = (entry->mHash & 0x7FFFFFFF) % mHashSize;
@ -3833,7 +3870,7 @@ String BfTypeUtils::TypeToString(BfTypeReference* typeRef)
return name; return name;
} }
BF_DBG_FATAL("Not implemented"); //BF_DBG_FATAL("Not implemented");
return typeRef->ToString(); return typeRef->ToString();
} }

View file

@ -374,6 +374,7 @@ enum BfTypeRebuildFlags
BfTypeRebuildFlag_SpecializedByAutocompleteMethod = 0x200, BfTypeRebuildFlag_SpecializedByAutocompleteMethod = 0x200,
BfTypeRebuildFlag_UnderlyingTypeDeferred = 0x400, BfTypeRebuildFlag_UnderlyingTypeDeferred = 0x400,
BfTypeRebuildFlag_TypeDataSaved = 0x800, BfTypeRebuildFlag_TypeDataSaved = 0x800,
BfTypeRebuildFlag_InTempPool = 0x1000
}; };
class BfTypeDIReplaceCallback; class BfTypeDIReplaceCallback;
@ -388,6 +389,26 @@ enum BfTypeDefineState : uint8
BfTypeDefineState_DefinedAndMethodsSlotted, BfTypeDefineState_DefinedAndMethodsSlotted,
}; };
class BfDelegateInfo
{
public:
Array<BfAstNode*> mDirectAllocNodes;
BfType* mReturnType;
Array<BfType*> mParams;
public:
BfDelegateInfo()
{
mReturnType = NULL;
}
~BfDelegateInfo()
{
for (auto directAllocNode : mDirectAllocNodes)
delete directAllocNode;
}
};
class BfType class BfType
{ {
public: public:
@ -469,6 +490,7 @@ public:
virtual bool IsFunction() { return false; } virtual bool IsFunction() { return false; }
virtual bool IsDelegateFromTypeRef() { return false; } virtual bool IsDelegateFromTypeRef() { return false; }
virtual bool IsFunctionFromTypeRef() { return false; } virtual bool IsFunctionFromTypeRef() { return false; }
virtual BfDelegateInfo* GetDelegateInfo() { return NULL; }
virtual bool IsValueType() { return false; } virtual bool IsValueType() { return false; }
virtual bool IsValueTypeOrValueTypePtr() { return false; } virtual bool IsValueTypeOrValueTypePtr() { return false; }
virtual bool IsWrappableType() { return false; } virtual bool IsWrappableType() { return false; }
@ -1969,20 +1991,15 @@ public:
class BfDelegateType : public BfTypeInstance class BfDelegateType : public BfTypeInstance
{ {
public: public:
Array<BfAstNode*> mDirectAllocNodes; BfDelegateInfo mDelegateInfo;
// These depend on the params in Invoke
bool mIsUnspecializedType; bool mIsUnspecializedType;
bool mIsUnspecializedTypeVariation; bool mIsUnspecializedTypeVariation;
BfType* mReturnType;
Array<BfType*> mParams;
public: public:
BfDelegateType() BfDelegateType()
{ {
mIsUnspecializedType = false; mIsUnspecializedType = false;
mIsUnspecializedTypeVariation = false; mIsUnspecializedTypeVariation = false;
mReturnType = NULL;
} }
~BfDelegateType(); ~BfDelegateType();
@ -1996,6 +2013,30 @@ public:
virtual bool IsUnspecializedType() override { return mIsUnspecializedType; } virtual bool IsUnspecializedType() override { return mIsUnspecializedType; }
virtual bool IsUnspecializedTypeVariation() override { return mIsUnspecializedTypeVariation; } virtual bool IsUnspecializedTypeVariation() override { return mIsUnspecializedTypeVariation; }
virtual BfDelegateInfo* GetDelegateInfo() { return &mDelegateInfo; }
};
class BfGenericDelegateType : public BfGenericTypeInstance
{
public:
BfDelegateInfo mDelegateInfo;
public:
BfGenericDelegateType()
{
}
virtual bool IsOnDemand() override { return true; }
virtual bool IsDelegate() override { return mTypeDef->mIsDelegate; }
virtual bool IsDelegateFromTypeRef() override { return mTypeDef->mIsDelegate; }
virtual bool IsFunction() override { return !mTypeDef->mIsDelegate; }
virtual bool IsFunctionFromTypeRef() override { return !mTypeDef->mIsDelegate; }
virtual BfDelegateInfo* GetDelegateInfo() override { return &mDelegateInfo; }
//virtual bool IsReified() override { return mIsReified; } //virtual bool IsReified() override { return mIsReified; }
}; };
@ -2318,7 +2359,7 @@ public:
} }
BfType* ResolveTypeRef(BfTypeReference* typeReference); BfType* ResolveTypeRef(BfTypeReference* typeReference);
BfTypeDef* ResolveToTypeDef(BfTypeReference* typeReference); BfTypeDef* ResolveToTypeDef(BfTypeReference* typeReference, BfType** outType = NULL);
}; };
public: public:
@ -2327,9 +2368,12 @@ public:
static bool GenericTypeEquals(BfGenericTypeInstance* lhsGenericType, BfTypeVector* typeGenericArguments, BfTypeReference* rhs, BfTypeDef* rhsTypeDef, LookupContext* ctx); static bool GenericTypeEquals(BfGenericTypeInstance* lhsGenericType, BfTypeVector* typeGenericArguments, BfTypeReference* rhs, BfTypeDef* rhsTypeDef, LookupContext* ctx);
static void HashGenericArguments(BfTypeReference* typeRef, LookupContext* ctx, int& hash); static void HashGenericArguments(BfTypeReference* typeRef, LookupContext* ctx, int& hash);
static int Hash(BfType* type, LookupContext* ctx, bool allowRef = false); static int Hash(BfType* type, LookupContext* ctx, bool allowRef = false);
static int DirectHash(BfTypeReference* typeRef, LookupContext* ctx, BfHashFlags flags = BfHashFlag_None);
static int Hash(BfTypeReference* typeRef, LookupContext* ctx, BfHashFlags flags = BfHashFlag_None); static int Hash(BfTypeReference* typeRef, LookupContext* ctx, BfHashFlags flags = BfHashFlag_None);
static bool Equals(BfType* lhs, BfType* rhs, LookupContext* ctx); static bool Equals(BfType* lhs, BfType* rhs, LookupContext* ctx);
static bool EqualsNoAlias(BfType* lhs, BfTypeReference* rhs, LookupContext* ctx);
static bool Equals(BfType* lhs, BfTypeReference* rhs, LookupContext* ctx); static bool Equals(BfType* lhs, BfTypeReference* rhs, LookupContext* ctx);
static bool Equals(BfType* lhs, BfTypeReference* rhs, BfTypeDef* rhsTypeDef, LookupContext* ctx);
public: public:
BfResolvedTypeSet() BfResolvedTypeSet()

View file

@ -0,0 +1,38 @@
#pragma warning disable 168
using System.Collections;
using System;
namespace Tests
{
class Aliases
{
class ClassA<T>
{
public typealias AliasA0 = int32;
public typealias AliasA1 = List<T>;
public typealias AliasA2<T2> = Dictionary<T, T2>;
public typealias AliasA3 = delegate T();
public typealias AliasA4<T2> = delegate T(T2 val);
public delegate T Zag();
}
[Test]
public static void TestBasics()
{
ClassA<float>.AliasA0 a0 = default;
a0 = 123;
ClassA<float>.AliasA1 list = scope List<float>();
Dictionary<float, int16> dict = scope ClassA<float>.AliasA2<int16>();
delegate double() dlg = default;
ClassA<double>.AliasA3 dlg2 = dlg;
delegate double(char8) dlg3 = default;
ClassA<double>.AliasA4<char8> dlg4 = dlg3;
}
}
}