1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 04:22:20 +02:00

Changed ResolveSelfType to not require a type instance

This commit is contained in:
Brian Fiete 2022-04-30 08:10:57 -07:00
parent 1bacf5eee2
commit 717d6aa4cf
6 changed files with 71 additions and 61 deletions

View file

@ -369,7 +369,7 @@ bool BfGenericInferContext::InferGenericArgument(BfMethodInstance* methodInstanc
{ {
if ((genericParamInst->mTypeConstraint != NULL) && (genericParamInst->mTypeConstraint->IsDelegate())) if ((genericParamInst->mTypeConstraint != NULL) && (genericParamInst->mTypeConstraint->IsDelegate()))
{ {
argType = mModule->ResolveGenericType(genericParamInst->mTypeConstraint, NULL, mCheckMethodGenericArguments); argType = mModule->ResolveGenericType(genericParamInst->mTypeConstraint, NULL, mCheckMethodGenericArguments, mModule->mCurTypeInstance);
if (argType == NULL) if (argType == NULL)
return true; return true;
} }
@ -855,7 +855,7 @@ void BfMethodMatcher::CompareMethods(BfMethodInstance* prevMethodInstance, BfTyp
bool paramWasUnspecialized = paramType->IsUnspecializedType(); bool paramWasUnspecialized = paramType->IsUnspecializedType();
if ((genericArgumentsSubstitute != NULL) && (paramWasUnspecialized)) if ((genericArgumentsSubstitute != NULL) && (paramWasUnspecialized))
{ {
paramType = mModule->ResolveGenericType(paramType, NULL, genericArgumentsSubstitute, allowSpecializeFail); paramType = mModule->ResolveGenericType(paramType, NULL, genericArgumentsSubstitute, mModule->mCurTypeInstance, allowSpecializeFail);
paramType = mModule->FixIntUnknown(paramType); paramType = mModule->FixIntUnknown(paramType);
} }
if (paramType->IsConstExprValue()) if (paramType->IsConstExprValue())
@ -867,7 +867,7 @@ void BfMethodMatcher::CompareMethods(BfMethodInstance* prevMethodInstance, BfTyp
bool prevParamWasUnspecialized = prevParamType->IsUnspecializedType(); bool prevParamWasUnspecialized = prevParamType->IsUnspecializedType();
if ((prevGenericArgumentsSubstitute != NULL) && (prevParamWasUnspecialized)) if ((prevGenericArgumentsSubstitute != NULL) && (prevParamWasUnspecialized))
{ {
prevParamType = mModule->ResolveGenericType(prevParamType, NULL, prevGenericArgumentsSubstitute, allowSpecializeFail); prevParamType = mModule->ResolveGenericType(prevParamType, NULL, prevGenericArgumentsSubstitute, mModule->mCurTypeInstance, allowSpecializeFail);
prevParamType = mModule->FixIntUnknown(prevParamType); prevParamType = mModule->FixIntUnknown(prevParamType);
} }
if (prevParamType->IsConstExprValue()) if (prevParamType->IsConstExprValue())
@ -1359,7 +1359,7 @@ BfTypedValue BfMethodMatcher::ResolveArgTypedValue(BfResolvedArg& resolvedArg, B
bool success = false; bool success = false;
(*genericArgumentsSubstitute)[returnMethodGenericArgIdx] = mModule->GetPrimitiveType(BfTypeCode_None); (*genericArgumentsSubstitute)[returnMethodGenericArgIdx] = mModule->GetPrimitiveType(BfTypeCode_None);
auto tryType = mModule->ResolveGenericType(origCheckType, NULL, genericArgumentsSubstitute); auto tryType = mModule->ResolveGenericType(origCheckType, NULL, genericArgumentsSubstitute, mModule->mCurTypeInstance);
if (tryType != NULL) if (tryType != NULL)
{ {
auto inferredReturnType = mModule->CreateValueFromExpression(lambdaBindExpr, tryType, (BfEvalExprFlags)((mBfEvalExprFlags & BfEvalExprFlags_InheritFlags) | BfEvalExprFlags_NoCast | BfEvalExprFlags_InferReturnType | BfEvalExprFlags_NoAutoComplete)); auto inferredReturnType = mModule->CreateValueFromExpression(lambdaBindExpr, tryType, (BfEvalExprFlags)((mBfEvalExprFlags & BfEvalExprFlags_InheritFlags) | BfEvalExprFlags_NoCast | BfEvalExprFlags_InferReturnType | BfEvalExprFlags_NoAutoComplete));
@ -1369,7 +1369,7 @@ BfTypedValue BfMethodMatcher::ResolveArgTypedValue(BfResolvedArg& resolvedArg, B
if (((flags & BfResolveArgFlag_FromGenericParam) != 0) && (lambdaBindExpr->mNewToken == NULL)) if (((flags & BfResolveArgFlag_FromGenericParam) != 0) && (lambdaBindExpr->mNewToken == NULL))
{ {
auto resolvedType = mModule->ResolveGenericType(origCheckType, NULL, genericArgumentsSubstitute); auto resolvedType = mModule->ResolveGenericType(origCheckType, NULL, genericArgumentsSubstitute, mModule->mCurTypeInstance);
if (resolvedType != NULL) if (resolvedType != NULL)
{ {
// Resolve for real // Resolve for real
@ -1439,7 +1439,7 @@ BfTypedValue BfMethodMatcher::ResolveArgTypedValue(BfResolvedArg& resolvedArg, B
} }
if ((genericArgumentsSubstitute != NULL) && (expectType->IsUnspecializedType())) if ((genericArgumentsSubstitute != NULL) && (expectType->IsUnspecializedType()))
expectType = mModule->ResolveGenericType(expectType, NULL, genericArgumentsSubstitute, true); expectType = mModule->ResolveGenericType(expectType, NULL, genericArgumentsSubstitute, mModule->mCurTypeInstance, true);
} }
exprEvaluator.mExpectingType = expectType; exprEvaluator.mExpectingType = expectType;
@ -1538,13 +1538,13 @@ bool BfMethodMatcher::InferFromGenericConstraints(BfMethodInstance* methodInstan
{ {
auto leftType = checkOpConstraint.mLeftType; auto leftType = checkOpConstraint.mLeftType;
if ((leftType != NULL) && (leftType->IsUnspecializedType())) if ((leftType != NULL) && (leftType->IsUnspecializedType()))
leftType = mModule->ResolveGenericType(leftType, NULL, methodGenericArgs); leftType = mModule->ResolveGenericType(leftType, NULL, methodGenericArgs, mModule->mCurTypeInstance);
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, NULL, methodGenericArgs); rightType = mModule->ResolveGenericType(rightType, NULL, methodGenericArgs, mModule->mCurTypeInstance);
if (rightType != NULL) if (rightType != NULL)
rightType = mModule->FixIntUnknown(rightType); rightType = mModule->FixIntUnknown(rightType);
@ -1857,7 +1857,7 @@ bool BfMethodMatcher::CheckMethod(BfTypeInstance* targetTypeInstance, BfTypeInst
if ((checkType != NULL) && (genericArgumentsSubstitute != NULL) && (checkType->IsUnspecializedType())) if ((checkType != NULL) && (genericArgumentsSubstitute != NULL) && (checkType->IsUnspecializedType()))
{ {
attemptedGenericResolve = true; attemptedGenericResolve = true;
checkType = mModule->ResolveGenericType(origCheckType, NULL, genericArgumentsSubstitute); checkType = mModule->ResolveGenericType(origCheckType, NULL, genericArgumentsSubstitute, mModule->mCurTypeInstance);
} }
if (wantType->IsUnspecializedType()) if (wantType->IsUnspecializedType())
@ -2068,7 +2068,7 @@ bool BfMethodMatcher::CheckMethod(BfTypeInstance* targetTypeInstance, BfTypeInst
goto NoMatch; goto NoMatch;
auto paramsArrayType = methodInstance->GetParamType(paramIdx); auto paramsArrayType = methodInstance->GetParamType(paramIdx);
paramsArrayType = mModule->ResolveGenericType(paramsArrayType, NULL, genericArgumentsSubstitute); paramsArrayType = mModule->ResolveGenericType(paramsArrayType, NULL, genericArgumentsSubstitute, mModule->mCurTypeInstance);
if (paramsArrayType == NULL) if (paramsArrayType == NULL)
goto NoMatch; goto NoMatch;
@ -2436,7 +2436,7 @@ NoMatch:
if (checkMethod->mMethodType == BfMethodType_Extension) if (checkMethod->mMethodType == BfMethodType_Extension)
{ {
auto thisParam = methodInstance->GetParamType(0); auto thisParam = methodInstance->GetParamType(0);
auto resolveThisParam = mModule->ResolveGenericType(thisParam, NULL, &mCheckMethodGenericArguments); auto resolveThisParam = mModule->ResolveGenericType(thisParam, NULL, &mCheckMethodGenericArguments, mModule->mCurTypeInstance);
if (resolveThisParam == NULL) if (resolveThisParam == NULL)
return false; return false;
if (!mModule->CanCast(mTarget, resolveThisParam, if (!mModule->CanCast(mTarget, resolveThisParam,
@ -7400,8 +7400,7 @@ BfTypedValue BfExprEvaluator::CreateCall(BfAstNode* targetSrc, const BfTypedValu
// Resolve `Self` types // Resolve `Self` types
if (wantType->IsUnspecializedTypeVariation()) if (wantType->IsUnspecializedTypeVariation())
{ {
SetAndRestoreValue<BfTypeInstance*> prevCurTypeInst(mModule->mCurTypeInstance, methodInstance->GetOwner()); wantType = mModule->ResolveSelfType(wantType, methodInstance->GetOwner());
wantType = mModule->ResolveGenericType(wantType, NULL, NULL);
} }
} }
@ -10119,7 +10118,7 @@ BfTypedValue BfExprEvaluator::MatchMethod(BfAstNode* targetSrc, BfMethodBoundExp
if (result) if (result)
{ {
if (result.mType->IsRef()) if (result.mType->IsRef())
result = mModule->RemoveRef(result); result = mModule->RemoveRef(result);
} }
PerformCallChecks(moduleMethodInstance.mMethodInstance, targetSrc); PerformCallChecks(moduleMethodInstance.mMethodInstance, targetSrc);
@ -10219,8 +10218,7 @@ BfTypedValue BfExprEvaluator::MatchMethod(BfAstNode* targetSrc, BfMethodBoundExp
if ((selfType != NULL) && (!selfType->IsInterface())) if ((selfType != NULL) && (!selfType->IsInterface()))
{ {
SetAndRestoreValue<BfTypeInstance*> prevCurTypeInst(mModule->mCurTypeInstance, selfType->ToTypeInstance()); auto resolvedType = mModule->ResolveSelfType(result.mType, selfType);
auto resolvedType = mModule->ResolveGenericType(result.mType, NULL, NULL);
if ((resolvedType != NULL) && (resolvedType != result.mType)) if ((resolvedType != NULL) && (resolvedType != result.mType))
result = mModule->GetDefaultTypedValue(resolvedType); result = mModule->GetDefaultTypedValue(resolvedType);
} }
@ -12178,7 +12176,7 @@ bool BfExprEvaluator::CanBindDelegate(BfDelegateBindExpression* delegateBindExpr
{ {
if (!isGenericMatch) if (!isGenericMatch)
return type; return type;
auto fixedType = mModule->ResolveGenericType(type, NULL, methodGenericArgumentsSubstitute); auto fixedType = mModule->ResolveGenericType(type, NULL, methodGenericArgumentsSubstitute, mModule->mCurTypeInstance);
if (fixedType != NULL) if (fixedType != NULL)
return fixedType; return fixedType;
return (BfType*)mModule->GetPrimitiveType(BfTypeCode_Var); return (BfType*)mModule->GetPrimitiveType(BfTypeCode_Var);
@ -15952,7 +15950,7 @@ BfModuleMethodInstance BfExprEvaluator::GetSelectedMethod(BfAstNode* targetSrc,
if ((genericParam->mTypeConstraint != NULL) && (genericParam->mTypeConstraint->IsDelegate())) if ((genericParam->mTypeConstraint != NULL) && (genericParam->mTypeConstraint->IsDelegate()))
{ {
// The only other option was to bind to a MethodRef // The only other option was to bind to a MethodRef
genericArg = mModule->ResolveGenericType(genericParam->mTypeConstraint, NULL, &methodMatcher.mBestMethodGenericArguments); genericArg = mModule->ResolveGenericType(genericParam->mTypeConstraint, NULL, &methodMatcher.mBestMethodGenericArguments, mModule->mCurTypeInstance);
} }
else else
{ {
@ -16137,7 +16135,8 @@ BfModuleMethodInstance BfExprEvaluator::GetSelectedMethod(BfAstNode* targetSrc,
typeGenericArgs = &curTypeInst->mGenericTypeInfo->mTypeGenericArguments; typeGenericArgs = &curTypeInst->mGenericTypeInfo->mTypeGenericArguments;
} }
BfType* specializedReturnType = mModule->ResolveGenericType(typeUnspecMethodInstance->mReturnType, typeGenericArgs, &methodMatcher.mBestMethodGenericArguments); BfType* specializedReturnType = mModule->ResolveGenericType(typeUnspecMethodInstance->mReturnType, typeGenericArgs, &methodMatcher.mBestMethodGenericArguments,
mModule->mCurTypeInstance);
if (specializedReturnType != NULL) if (specializedReturnType != NULL)
*overrideReturnType = specializedReturnType; *overrideReturnType = specializedReturnType;
} }
@ -22999,7 +22998,8 @@ void BfExprEvaluator::PerformBinaryOperation(BfAstNode* leftExpression, BfAstNod
if (methodMatcher.CheckMethod(NULL, checkType, operatorDef, false)) if (methodMatcher.CheckMethod(NULL, checkType, operatorDef, false))
{ {
auto rawMethodInstance = mModule->GetRawMethodInstance(checkType, operatorDef); auto rawMethodInstance = mModule->GetRawMethodInstance(checkType, operatorDef);
auto returnType = mModule->ResolveGenericType(rawMethodInstance->mReturnType, NULL, &methodMatcher.mBestMethodGenericArguments); auto returnType = mModule->ResolveGenericType(rawMethodInstance->mReturnType, NULL, &methodMatcher.mBestMethodGenericArguments,
mModule->mCurTypeInstance);
if (returnType != NULL) if (returnType != NULL)
{ {
operatorConstraintReturnType = returnType; operatorConstraintReturnType = returnType;
@ -23048,7 +23048,8 @@ void BfExprEvaluator::PerformBinaryOperation(BfAstNode* leftExpression, BfAstNod
if (methodMatcher.CheckMethod(NULL, checkType, oppositeOperatorDef, false)) if (methodMatcher.CheckMethod(NULL, checkType, oppositeOperatorDef, false))
{ {
auto rawMethodInstance = mModule->GetRawMethodInstance(checkType, oppositeOperatorDef); auto rawMethodInstance = mModule->GetRawMethodInstance(checkType, oppositeOperatorDef);
auto returnType = mModule->ResolveGenericType(rawMethodInstance->mReturnType, NULL, &methodMatcher.mBestMethodGenericArguments); auto returnType = mModule->ResolveGenericType(rawMethodInstance->mReturnType, NULL, &methodMatcher.mBestMethodGenericArguments,
mModule->mCurTypeInstance);
if (returnType != NULL) if (returnType != NULL)
{ {
operatorConstraintReturnType = returnType; operatorConstraintReturnType = returnType;

View file

@ -8302,7 +8302,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, NULL, methodGenericArgs); convCheckConstraint = ResolveGenericType(convCheckConstraint, NULL, methodGenericArgs, mCurTypeInstance);
if (convCheckConstraint == NULL) if (convCheckConstraint == NULL)
return false; return false;
if (((checkArgType->IsMethodRef()) || (checkArgType->IsFunction())) && (convCheckConstraint->IsDelegate())) if (((checkArgType->IsMethodRef()) || (checkArgType->IsFunction())) && (convCheckConstraint->IsDelegate()))
@ -8372,7 +8372,7 @@ bool BfModule::CheckGenericConstraints(const BfGenericParamSource& genericParamS
{ {
BfType* convCheckConstraint = checkConstraint; BfType* convCheckConstraint = checkConstraint;
if (convCheckConstraint->IsUnspecializedType()) if (convCheckConstraint->IsUnspecializedType())
convCheckConstraint = ResolveGenericType(convCheckConstraint, NULL, methodGenericArgs); convCheckConstraint = ResolveGenericType(convCheckConstraint, NULL, methodGenericArgs, mCurTypeInstance);
if (convCheckConstraint == NULL) if (convCheckConstraint == NULL)
return false; return false;
@ -8415,13 +8415,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, NULL, methodGenericArgs); leftType = ResolveGenericType(leftType, NULL, methodGenericArgs, mCurTypeInstance);
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, NULL, methodGenericArgs); rightType = ResolveGenericType(rightType, NULL, methodGenericArgs, mCurTypeInstance);
if (rightType != NULL) if (rightType != NULL)
rightType = FixIntUnknown(rightType); rightType = FixIntUnknown(rightType);
@ -10989,7 +10989,7 @@ StringT<128> BfModule::MethodToString(BfMethodInstance* methodInst, BfMethodName
BfType* type = methodInst->mMethodInstanceGroup->mOwner; BfType* type = methodInst->mMethodInstanceGroup->mOwner;
if ((hasGenericArgs) && (type->IsUnspecializedType())) if ((hasGenericArgs) && (type->IsUnspecializedType()))
type = ResolveGenericType(type, typeGenericArgs, methodGenericArgs); type = ResolveGenericType(type, typeGenericArgs, methodGenericArgs, mCurTypeInstance);
if ((type == NULL) || (!type->IsUnspecializedTypeVariation())) if ((type == NULL) || (!type->IsUnspecializedTypeVariation()))
typeNameFlags = BfTypeNameFlag_ResolveGenericParamNames; typeNameFlags = BfTypeNameFlag_ResolveGenericParamNames;
if (allowResolveGenericParamNames) if (allowResolveGenericParamNames)
@ -11003,7 +11003,7 @@ StringT<128> BfModule::MethodToString(BfMethodInstance* methodInst, BfMethodName
if (allowResolveGenericParamNames) if (allowResolveGenericParamNames)
typeNameFlags = BfTypeNameFlag_ResolveGenericParamNames; typeNameFlags = BfTypeNameFlag_ResolveGenericParamNames;
if ((hasGenericArgs) && (type->IsUnspecializedType())) if ((hasGenericArgs) && (type->IsUnspecializedType()))
type = ResolveGenericType(type, typeGenericArgs, methodGenericArgs); type = ResolveGenericType(type, typeGenericArgs, methodGenericArgs, mCurTypeInstance);
methodName += TypeToString(type, typeNameFlags); methodName += TypeToString(type, typeNameFlags);
}; };
@ -11174,7 +11174,7 @@ StringT<128> BfModule::MethodToString(BfMethodInstance* methodInst, BfMethodName
} }
if (type->IsUnspecializedType()) if (type->IsUnspecializedType())
type = ResolveGenericType(type, NULL, methodGenericArgs); type = ResolveGenericType(type, NULL, methodGenericArgs, mCurTypeInstance);
} }
if ((methodGenericArgs == NULL) && (mCurMethodInstance == NULL) && (mCurTypeInstance == NULL)) if ((methodGenericArgs == NULL) && (mCurMethodInstance == NULL) && (mCurTypeInstance == NULL))

View file

@ -1870,8 +1870,8 @@ public:
void EmitDeferredScopeCalls(bool useSrcPositions, BfScopeData* scope, BfIRBlock doneBlock = BfIRBlock()); void EmitDeferredScopeCalls(bool useSrcPositions, BfScopeData* scope, BfIRBlock doneBlock = BfIRBlock());
void MarkScopeLeft(BfScopeData* scopeData, bool isNoReturn = false); void MarkScopeLeft(BfScopeData* scopeData, bool isNoReturn = false);
BfGenericParamType* GetGenericParamType(BfGenericParamKind paramKind, int paramIdx); BfGenericParamType* GetGenericParamType(BfGenericParamKind paramKind, int paramIdx);
BfType* ResolveGenericType(BfType* unspecializedType, BfTypeVector* typeGenericArguments, BfTypeVector* methodGenericArguments, bool allowFail = false); BfType* ResolveGenericType(BfType* unspecializedType, BfTypeVector* typeGenericArguments, BfTypeVector* methodGenericArguments, BfType* selfType, bool allowFail = false);
BfType* ResolveSelfType(BfType* type, BfTypeInstance* selfType); BfType* ResolveSelfType(BfType* type, BfType* selfType);
bool IsUnboundGeneric(BfType* type); bool IsUnboundGeneric(BfType* type);
BfGenericParamInstance* GetGenericTypeParamInstance(int paramIdx); BfGenericParamInstance* GetGenericTypeParamInstance(int paramIdx);
BfGenericParamInstance* GetGenericParamInstance(BfGenericParamType* type); BfGenericParamInstance* GetGenericParamInstance(BfGenericParamType* type);

View file

@ -6450,7 +6450,7 @@ void BfModule::DoTypeInstanceMethodProcessing(BfTypeInstance* typeInstance)
auto iReturnType = ifaceMethodInst->mReturnType; auto iReturnType = ifaceMethodInst->mReturnType;
if (iReturnType->IsUnspecializedTypeVariation()) if (iReturnType->IsUnspecializedTypeVariation())
{ {
BfType* resolvedType = ResolveGenericType(iReturnType, NULL, NULL); BfType* resolvedType = ResolveGenericType(iReturnType, NULL, NULL, mCurTypeInstance);
if (resolvedType != NULL) if (resolvedType != NULL)
iReturnType = resolvedType; iReturnType = resolvedType;
else else
@ -8232,7 +8232,7 @@ BfTypeDef* BfModule::ResolveGenericInstanceDef(BfGenericInstanceTypeRef* generic
return NULL; return NULL;
} }
BfType* BfModule::ResolveGenericType(BfType* unspecializedType, BfTypeVector* typeGenericArguments, BfTypeVector* methodGenericArguments, bool allowFail) BfType* BfModule::ResolveGenericType(BfType* unspecializedType, BfTypeVector* typeGenericArguments, BfTypeVector* methodGenericArguments, BfType* selfType, bool allowFail)
{ {
if (unspecializedType->IsGenericParam()) if (unspecializedType->IsGenericParam())
{ {
@ -8260,8 +8260,8 @@ BfType* BfModule::ResolveGenericType(BfType* unspecializedType, BfTypeVector* ty
return unspecializedType; return unspecializedType;
} }
if ((unspecializedType->IsSelf()) && (mCurTypeInstance != NULL)) if ((unspecializedType->IsSelf()) && (selfType != NULL))
return mCurTypeInstance; return selfType;
if (!unspecializedType->IsUnspecializedType()) if (!unspecializedType->IsUnspecializedType())
{ {
@ -8271,12 +8271,12 @@ BfType* BfModule::ResolveGenericType(BfType* unspecializedType, BfTypeVector* ty
if (unspecializedType->IsUnknownSizedArrayType()) if (unspecializedType->IsUnknownSizedArrayType())
{ {
auto* arrayType = (BfUnknownSizedArrayType*)unspecializedType; auto* arrayType = (BfUnknownSizedArrayType*)unspecializedType;
auto elementType = ResolveGenericType(arrayType->mElementType, typeGenericArguments, methodGenericArguments, allowFail); auto elementType = ResolveGenericType(arrayType->mElementType, typeGenericArguments, methodGenericArguments, selfType, allowFail);
if (elementType == NULL) if (elementType == NULL)
return NULL; return NULL;
if (elementType->IsVar()) if (elementType->IsVar())
return elementType; return elementType;
auto sizeType = ResolveGenericType(arrayType->mElementCountSource, typeGenericArguments, methodGenericArguments, allowFail); auto sizeType = ResolveGenericType(arrayType->mElementCountSource, typeGenericArguments, methodGenericArguments, selfType, allowFail);
if (sizeType == NULL) if (sizeType == NULL)
return NULL; return NULL;
if (sizeType->IsConstExprValue()) if (sizeType->IsConstExprValue())
@ -8289,7 +8289,7 @@ BfType* BfModule::ResolveGenericType(BfType* unspecializedType, BfTypeVector* ty
if (unspecializedType->IsSizedArray()) if (unspecializedType->IsSizedArray())
{ {
auto* arrayType = (BfSizedArrayType*)unspecializedType; auto* arrayType = (BfSizedArrayType*)unspecializedType;
auto elementType = ResolveGenericType(arrayType->mElementType, typeGenericArguments, methodGenericArguments, allowFail); auto elementType = ResolveGenericType(arrayType->mElementType, typeGenericArguments, methodGenericArguments, selfType, allowFail);
if (elementType == NULL) if (elementType == NULL)
return NULL; return NULL;
if (elementType->IsVar()) if (elementType->IsVar())
@ -8301,7 +8301,7 @@ BfType* BfModule::ResolveGenericType(BfType* unspecializedType, BfTypeVector* ty
if (unspecializedType->IsRef()) if (unspecializedType->IsRef())
{ {
auto refType = (BfRefType*)unspecializedType; auto refType = (BfRefType*)unspecializedType;
auto elementType = ResolveGenericType(refType->GetUnderlyingType(), typeGenericArguments, methodGenericArguments, allowFail); auto elementType = ResolveGenericType(refType->GetUnderlyingType(), typeGenericArguments, methodGenericArguments, selfType, allowFail);
if (elementType == NULL) if (elementType == NULL)
return NULL; return NULL;
if (elementType->IsVar()) if (elementType->IsVar())
@ -8313,7 +8313,7 @@ BfType* BfModule::ResolveGenericType(BfType* unspecializedType, BfTypeVector* ty
if (unspecializedType->IsPointer()) if (unspecializedType->IsPointer())
{ {
auto ptrType = (BfPointerType*)unspecializedType; auto ptrType = (BfPointerType*)unspecializedType;
auto elementType = ResolveGenericType(ptrType->GetUnderlyingType(), typeGenericArguments, methodGenericArguments, allowFail); auto elementType = ResolveGenericType(ptrType->GetUnderlyingType(), typeGenericArguments, methodGenericArguments, selfType, allowFail);
if (elementType == NULL) if (elementType == NULL)
return NULL; return NULL;
if (elementType->IsVar()) if (elementType->IsVar())
@ -8325,7 +8325,7 @@ BfType* BfModule::ResolveGenericType(BfType* unspecializedType, BfTypeVector* ty
if (unspecializedType->IsConcreteInterfaceType()) if (unspecializedType->IsConcreteInterfaceType())
{ {
auto concreteType = (BfConcreteInterfaceType*)unspecializedType; auto concreteType = (BfConcreteInterfaceType*)unspecializedType;
auto elementType = ResolveGenericType(concreteType->GetUnderlyingType(), typeGenericArguments, methodGenericArguments, allowFail); auto elementType = ResolveGenericType(concreteType->GetUnderlyingType(), typeGenericArguments, methodGenericArguments, selfType, allowFail);
if (elementType == NULL) if (elementType == NULL)
return NULL; return NULL;
auto elementTypeInstance = elementType->ToTypeInstance(); auto elementTypeInstance = elementType->ToTypeInstance();
@ -8337,7 +8337,7 @@ BfType* BfModule::ResolveGenericType(BfType* unspecializedType, BfTypeVector* ty
if (unspecializedType->IsArray()) if (unspecializedType->IsArray())
{ {
auto arrayType = (BfArrayType*)unspecializedType; auto arrayType = (BfArrayType*)unspecializedType;
auto elementType = ResolveGenericType(arrayType->GetUnderlyingType(), typeGenericArguments, methodGenericArguments, allowFail); auto elementType = ResolveGenericType(arrayType->GetUnderlyingType(), typeGenericArguments, methodGenericArguments, selfType, allowFail);
if (elementType == NULL) if (elementType == NULL)
return NULL; return NULL;
if (elementType->IsVar()) if (elementType->IsVar())
@ -8361,7 +8361,7 @@ BfType* BfModule::ResolveGenericType(BfType* unspecializedType, BfTypeVector* ty
{ {
fieldNames.push_back(fieldInstance.GetFieldDef()->mName); fieldNames.push_back(fieldInstance.GetFieldDef()->mName);
auto origGenericArg = fieldInstance.mResolvedType; auto origGenericArg = fieldInstance.mResolvedType;
auto newGenericArg = ResolveGenericType(origGenericArg, typeGenericArguments, methodGenericArguments, allowFail); auto newGenericArg = ResolveGenericType(origGenericArg, typeGenericArguments, methodGenericArguments, selfType, allowFail);
if (newGenericArg == NULL) if (newGenericArg == NULL)
return NULL; return NULL;
if (newGenericArg->IsVar()) if (newGenericArg->IsVar())
@ -8400,7 +8400,7 @@ BfType* BfModule::ResolveGenericType(BfType* unspecializedType, BfTypeVector* ty
BfType* resolvedArg = unspecializedGenericTupleType->mGenericTypeInfo->mTypeGenericArguments[genericArgIdx]; BfType* resolvedArg = unspecializedGenericTupleType->mGenericTypeInfo->mTypeGenericArguments[genericArgIdx];
if (resolvedArg->IsUnspecializedType()) if (resolvedArg->IsUnspecializedType())
{ {
resolvedArg = ResolveGenericType(resolvedArg, typeGenericArguments, methodGenericArguments, allowFail); resolvedArg = ResolveGenericType(resolvedArg, typeGenericArguments, methodGenericArguments, selfType, allowFail);
if (resolvedArg == NULL) if (resolvedArg == NULL)
return NULL; return NULL;
if (resolvedArg->IsVar()) if (resolvedArg->IsVar())
@ -8505,7 +8505,7 @@ BfType* BfModule::ResolveGenericType(BfType* unspecializedType, BfTypeVector* ty
bool failed = false; bool failed = false;
bool hasTypeGenerics = false; bool hasTypeGenerics = false;
auto returnType = ResolveGenericType(unspecializedDelegateInfo->mReturnType, typeGenericArguments, methodGenericArguments, allowFail); auto returnType = ResolveGenericType(unspecializedDelegateInfo->mReturnType, typeGenericArguments, methodGenericArguments, selfType, allowFail);
if (returnType == NULL) if (returnType == NULL)
return NULL; return NULL;
@ -8518,7 +8518,7 @@ BfType* BfModule::ResolveGenericType(BfType* unspecializedType, BfTypeVector* ty
Array<BfType*> paramTypes; Array<BfType*> paramTypes;
for (auto param : unspecializedDelegateInfo->mParams) for (auto param : unspecializedDelegateInfo->mParams)
{ {
auto paramType = ResolveGenericType(param, typeGenericArguments, methodGenericArguments, allowFail); auto paramType = ResolveGenericType(param, typeGenericArguments, methodGenericArguments, selfType, allowFail);
if (paramType == NULL) if (paramType == NULL)
return NULL; return NULL;
if (paramType->IsVar()) if (paramType->IsVar())
@ -8544,7 +8544,7 @@ BfType* BfModule::ResolveGenericType(BfType* unspecializedType, BfTypeVector* ty
BfType* resolvedArg = unspecializedGenericDelegateType->mGenericTypeInfo->mTypeGenericArguments[genericArgIdx]; BfType* resolvedArg = unspecializedGenericDelegateType->mGenericTypeInfo->mTypeGenericArguments[genericArgIdx];
if (resolvedArg->IsUnspecializedType()) if (resolvedArg->IsUnspecializedType())
{ {
resolvedArg = ResolveGenericType(resolvedArg, typeGenericArguments, methodGenericArguments, allowFail); resolvedArg = ResolveGenericType(resolvedArg, typeGenericArguments, methodGenericArguments, selfType, allowFail);
if (resolvedArg == NULL) if (resolvedArg == NULL)
return NULL; return NULL;
if (resolvedArg->IsVar()) if (resolvedArg->IsVar())
@ -8696,7 +8696,7 @@ BfType* BfModule::ResolveGenericType(BfType* unspecializedType, BfTypeVector* ty
{ {
if (genericArg->IsUnspecializedType()) if (genericArg->IsUnspecializedType())
{ {
auto resolvedArg = ResolveGenericType(genericArg, typeGenericArguments, methodGenericArguments, allowFail); auto resolvedArg = ResolveGenericType(genericArg, typeGenericArguments, methodGenericArguments, selfType, allowFail);
if (resolvedArg == NULL) if (resolvedArg == NULL)
return NULL; return NULL;
if (resolvedArg->IsVar()) if (resolvedArg->IsVar())
@ -8723,12 +8723,11 @@ BfType* BfModule::ResolveGenericType(BfType* unspecializedType, BfTypeVector* ty
return unspecializedType; return unspecializedType;
} }
BfType* BfModule::ResolveSelfType(BfType* type, BfTypeInstance* selfType) BfType* BfModule::ResolveSelfType(BfType* type, BfType* selfType)
{ {
if (!type->IsUnspecializedTypeVariation()) if (!type->IsUnspecializedTypeVariation())
return type; return type;
SetAndRestoreValue<BfTypeInstance*> prevCurTypeInst(mCurTypeInstance, selfType); return ResolveGenericType(type, NULL, NULL, selfType);
return ResolveGenericType(type, NULL, NULL);
} }
BfType* BfModule::ResolveType(BfType* lookupType, BfPopulateType populateType, BfResolveTypeRefFlags resolveFlags) BfType* BfModule::ResolveType(BfType* lookupType, BfPopulateType populateType, BfResolveTypeRefFlags resolveFlags)
@ -11250,12 +11249,12 @@ BfType* BfModule::ResolveTypeRef(BfTypeReference* typeRef, BfPopulateType popula
((type->IsDelegateFromTypeRef()) || (type->IsFunctionFromTypeRef()))) ((type->IsDelegateFromTypeRef()) || (type->IsFunctionFromTypeRef())))
{ {
mContext->mResolvedTypes.RemoveEntry(resolvedEntry); mContext->mResolvedTypes.RemoveEntry(resolvedEntry);
return ResolveGenericType(type, &genericArgs, NULL); return ResolveGenericType(type, &genericArgs, NULL, mCurTypeInstance);
} }
else if ((type != NULL) && (type->IsTuple())) else if ((type != NULL) && (type->IsTuple()))
{ {
mContext->mResolvedTypes.RemoveEntry(resolvedEntry); mContext->mResolvedTypes.RemoveEntry(resolvedEntry);
return ResolveGenericType(type, &genericArgs, NULL); return ResolveGenericType(type, &genericArgs, NULL, mCurTypeInstance);
} }
else if ((typeDef != NULL) && (typeDef->mTypeCode == BfTypeCode_TypeAlias)) else if ((typeDef != NULL) && (typeDef->mTypeCode == BfTypeCode_TypeAlias))
{ {

View file

@ -5242,15 +5242,9 @@ String BfTypeUtils::TypeToString(BfAstNode* typeRefNode)
bool BfTypeUtils::TypeEquals(BfType* typeA, BfType* typeB, BfTypeInstance* selfType) bool BfTypeUtils::TypeEquals(BfType* typeA, BfType* typeB, BfTypeInstance* selfType)
{ {
if (typeA->IsUnspecializedTypeVariation()) if (typeA->IsUnspecializedTypeVariation())
{ typeA = selfType->mModule->ResolveSelfType(typeA, selfType);
SetAndRestoreValue<BfTypeInstance*> prevCurTypeInst(selfType->mModule->mCurTypeInstance, selfType);
typeA = selfType->mModule->ResolveGenericType(typeA, NULL, NULL);
}
if (typeB->IsUnspecializedTypeVariation()) if (typeB->IsUnspecializedTypeVariation())
{ typeB = selfType->mModule->ResolveSelfType(typeB, selfType);
SetAndRestoreValue<BfTypeInstance*> prevCurTypeInst(selfType->mModule->mCurTypeInstance, selfType);
typeB = selfType->mModule->ResolveGenericType(typeB, NULL, NULL);
}
return typeA == typeB; return typeA == typeB;
} }

View file

@ -389,6 +389,22 @@ namespace Tests
} }
} }
class Test5
{
public Result<T> TryGetValue<T>() where T : IParseable
{
T val = default;
var s = T.Serialize(val);
return T.Deserialize(s);
}
}
interface IParseable
{
public static Result<Self> Deserialize(StringView value);
public static String Serialize(Self value);
}
[Test] [Test]
public static void TestDefaults() public static void TestDefaults()
{ {