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

Allow comptime extern constraint typerefs

This commit is contained in:
Brian Fiete 2021-11-23 09:12:10 -08:00
parent 6fe2a7002a
commit 12a3ba937a
5 changed files with 54 additions and 18 deletions

View file

@ -1557,21 +1557,9 @@ bool BfMethodMatcher::InferFromGenericConstraints(BfMethodInstance* methodInstan
{ {
for (auto comptypeConstraint : genericParamInst->mComptypeConstraint) for (auto comptypeConstraint : genericParamInst->mComptypeConstraint)
{ {
BfConstraintState constraintSet; checkArgType = mModule->ResolveGenericMethodTypeRef(comptypeConstraint, methodInstance, genericParamInst, methodGenericArgs);
constraintSet.mPrevState = mModule->mContext->mCurConstraintState; if (checkArgType == NULL)
constraintSet.mGenericParamInstance = genericParamInst;
constraintSet.mMethodInstance = methodInstance;
constraintSet.mMethodGenericArgsOverride = methodGenericArgs;
SetAndRestoreValue<BfConstraintState*> prevConstraintSet(mModule->mContext->mCurConstraintState, &constraintSet);
if (!mModule->CheckConstraintState(NULL))
return false; return false;
SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(mModule->mCurMethodInstance, methodInstance);
SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(mModule->mCurTypeInstance, methodInstance->GetOwner());
SetAndRestoreValue<bool> prevIgnoreErrors(mModule->mIgnoreErrors, true);
checkArgType = mModule->ResolveTypeRef(comptypeConstraint);
} }
} }
@ -2138,7 +2126,21 @@ bool BfMethodMatcher::CheckMethod(BfTypeInstance* targetTypeInstance, BfTypeInst
{ {
auto genericParam = methodInstance->mMethodInfoEx->mGenericParams[checkMethod->mGenericParams.size() + externConstraintIdx]; auto genericParam = methodInstance->mMethodInfoEx->mGenericParams[checkMethod->mGenericParams.size() + externConstraintIdx];
BF_ASSERT(genericParam->mExternType != NULL); BF_ASSERT(genericParam->mExternType != NULL);
if (!mModule->CheckGenericConstraints(BfGenericParamSource(methodInstance), genericParam->mExternType, NULL, genericParam, genericArgumentsSubstitute, NULL)) auto externType = genericParam->mExternType;
BfTypeVector* externGenericArgumentsSubstitute = genericArgumentsSubstitute;
if (externType->IsVar())
{
auto& externConstraint = checkMethod->mExternalConstraints[externConstraintIdx];
if (externConstraint.mTypeRef != NULL)
{
externType = mModule->ResolveGenericMethodTypeRef(externConstraint.mTypeRef, methodInstance, genericParam, genericArgumentsSubstitute);
if (externType == NULL)
goto NoMatch;
}
}
if (!mModule->CheckGenericConstraints(BfGenericParamSource(methodInstance), externType, NULL, genericParam, externGenericArgumentsSubstitute, NULL))
goto NoMatch; goto NoMatch;
} }

View file

@ -21956,6 +21956,8 @@ void BfModule::DoMethodDeclaration(BfMethodDeclaration* methodDeclaration, bool
for (auto genericParam : methodInstance->mMethodInfoEx->mGenericParams) for (auto genericParam : methodInstance->mMethodInfoEx->mGenericParams)
{ {
if (!genericParam->mExternType->IsGenericParam())
AddDependency(genericParam->mExternType, mCurTypeInstance, BfDependencyMap::DependencyFlag_Constraint);
for (auto constraintTypeInst : genericParam->mInterfaceConstraints) for (auto constraintTypeInst : genericParam->mInterfaceConstraints)
AddDependency(constraintTypeInst, mCurTypeInstance, BfDependencyMap::DependencyFlag_Constraint); AddDependency(constraintTypeInst, mCurTypeInstance, BfDependencyMap::DependencyFlag_Constraint);
if (genericParam->mTypeConstraint != NULL) if (genericParam->mTypeConstraint != NULL)

View file

@ -1708,6 +1708,7 @@ public:
bool InitGenericParams(BfType* resolvedTypeRef); bool InitGenericParams(BfType* resolvedTypeRef);
bool FinishGenericParams(BfType* resolvedTypeRef); bool FinishGenericParams(BfType* resolvedTypeRef);
bool ValidateGenericConstraints(BfTypeReference* typeRef, BfTypeInstance* genericTypeInstance, bool ignoreErrors); bool ValidateGenericConstraints(BfTypeReference* typeRef, BfTypeInstance* genericTypeInstance, bool ignoreErrors);
BfType* ResolveGenericMethodTypeRef(BfTypeReference* typeRef, BfMethodInstance* methodInstance, BfGenericParamInstance* genericParamInstance, BfTypeVector* methodGenericArgsOverride);
bool AreConstraintsSubset(BfGenericParamInstance* checkInner, BfGenericParamInstance* checkOuter); bool AreConstraintsSubset(BfGenericParamInstance* checkInner, BfGenericParamInstance* checkOuter);
bool CheckConstraintState(BfAstNode* refNode); bool CheckConstraintState(BfAstNode* refNode);
bool ShouldAllowMultipleDefinitions(BfTypeInstance* typeInst, BfTypeDef* firstDeclaringTypeDef, BfTypeDef* secondDeclaringTypeDef); bool ShouldAllowMultipleDefinitions(BfTypeInstance* typeInst, BfTypeDef* firstDeclaringTypeDef, BfTypeDef* secondDeclaringTypeDef);

View file

@ -109,6 +109,8 @@ BfGenericExtensionEntry* BfModule::BuildGenericExtensionInfo(BfTypeInstance* gen
for (auto genericParam : genericExEntry->mGenericParams) for (auto genericParam : genericExEntry->mGenericParams)
{ {
if (!genericParam->mExternType->IsGenericParam())
AddDependency(genericParam->mExternType, mCurTypeInstance, BfDependencyMap::DependencyFlag_Constraint);
for (auto constraintTypeInst : genericParam->mInterfaceConstraints) for (auto constraintTypeInst : genericParam->mInterfaceConstraints)
AddDependency(constraintTypeInst, mCurTypeInstance, BfDependencyMap::DependencyFlag_Constraint); AddDependency(constraintTypeInst, mCurTypeInstance, BfDependencyMap::DependencyFlag_Constraint);
if (genericParam->mTypeConstraint != NULL) if (genericParam->mTypeConstraint != NULL)
@ -301,6 +303,8 @@ bool BfModule::FinishGenericParams(BfType* resolvedTypeRef)
for (auto genericParam : genericTypeInst->mGenericTypeInfo->mGenericParams) for (auto genericParam : genericTypeInst->mGenericTypeInfo->mGenericParams)
{ {
if (!genericParam->mExternType->IsGenericParam())
AddDependency(genericParam->mExternType, mCurTypeInstance, BfDependencyMap::DependencyFlag_Constraint);
for (auto constraintTypeInst : genericParam->mInterfaceConstraints) for (auto constraintTypeInst : genericParam->mInterfaceConstraints)
AddDependency(constraintTypeInst, mCurTypeInstance, BfDependencyMap::DependencyFlag_Constraint); AddDependency(constraintTypeInst, mCurTypeInstance, BfDependencyMap::DependencyFlag_Constraint);
if (genericParam->mTypeConstraint != NULL) if (genericParam->mTypeConstraint != NULL)
@ -377,6 +381,28 @@ bool BfModule::ValidateGenericConstraints(BfTypeReference* typeRef, BfTypeInstan
return true; return true;
} }
BfType* BfModule::ResolveGenericMethodTypeRef(BfTypeReference* typeRef, BfMethodInstance* methodInstance, BfGenericParamInstance* genericParamInstance, BfTypeVector* methodGenericArgsOverride)
{
BfConstraintState constraintSet;
constraintSet.mPrevState = mContext->mCurConstraintState;
constraintSet.mGenericParamInstance = genericParamInstance;
constraintSet.mMethodInstance = methodInstance;
constraintSet.mMethodGenericArgsOverride = methodGenericArgsOverride;
SetAndRestoreValue<BfConstraintState*> prevConstraintSet(mContext->mCurConstraintState, &constraintSet);
if (!CheckConstraintState(NULL))
return NULL;
SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(mCurMethodInstance, methodInstance);
SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(mCurTypeInstance, methodInstance->GetOwner());
SetAndRestoreValue<bool> prevIgnoreErrors(mIgnoreErrors, true);
BfType* type = ResolveTypeRef(typeRef);
if (type == NULL)
type = GetPrimitiveType(BfTypeCode_Var);
return type;
}
bool BfModule::AreConstraintsSubset(BfGenericParamInstance* checkInner, BfGenericParamInstance* checkOuter) bool BfModule::AreConstraintsSubset(BfGenericParamInstance* checkInner, BfGenericParamInstance* checkOuter)
{ {
if (checkOuter == NULL) if (checkOuter == NULL)

View file

@ -572,7 +572,12 @@ void BfSourceClassifier::Visit(BfMethodDeclaration* methodDeclaration)
{ {
BfTypeReference* typeRef = genericConstraint->mTypeRef; BfTypeReference* typeRef = genericConstraint->mTypeRef;
if (typeRef != NULL) if (typeRef != NULL)
SetElementType(typeRef, BfSourceElementType_GenericParam); {
if (auto namedTypeRef = BfNodeDynCast<BfNamedTypeReference>(typeRef))
SetElementType(typeRef, BfSourceElementType_GenericParam);
else
VisitChild(typeRef);
}
} }
} }
} }