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

Fixed repr for on-demand delegate types, avoiding circular Event refs

This commit is contained in:
Brian Fiete 2020-01-23 12:02:54 -08:00
parent ddc943876d
commit 95e4c1b3ca
10 changed files with 39 additions and 78 deletions

View file

@ -196,11 +196,6 @@ void BfStructuralVisitor::Visit(BfCollectionInitializerExpression* collectionIni
Visit(collectionInitExpr->ToBase()); Visit(collectionInitExpr->ToBase());
} }
void BfStructuralVisitor::Visit(BfArraySizeSpecifier* arraySizeSpecifier)
{
Visit(arraySizeSpecifier->ToBase());
}
void BfStructuralVisitor::Visit(BfTypeReference* typeRef) void BfStructuralVisitor::Visit(BfTypeReference* typeRef)
{ {
Visit(typeRef->ToBase()); Visit(typeRef->ToBase());

View file

@ -351,7 +351,6 @@ class BfDefaultExpression;
class BfUninitializedExpression; class BfUninitializedExpression;
class BfConditionalExpression; class BfConditionalExpression;
class BfCollectionInitializerExpression; class BfCollectionInitializerExpression;
class BfArraySizeSpecifier;
class BfSizedArrayCreateExpression; class BfSizedArrayCreateExpression;
class BfEmptyStatement; class BfEmptyStatement;
class BfGenericOperatorConstraint; class BfGenericOperatorConstraint;
@ -432,8 +431,7 @@ public:
virtual void Visit(BfBaseExpression* baseExpr); virtual void Visit(BfBaseExpression* baseExpr);
virtual void Visit(BfMixinExpression* thisExpr); virtual void Visit(BfMixinExpression* thisExpr);
virtual void Visit(BfSizedArrayCreateExpression* createExpr); virtual void Visit(BfSizedArrayCreateExpression* createExpr);
virtual void Visit(BfCollectionInitializerExpression* collectionInitExpr); virtual void Visit(BfCollectionInitializerExpression* collectionInitExpr);
virtual void Visit(BfArraySizeSpecifier* arraySizeSpecifier);
virtual void Visit(BfTypeReference* typeRef); virtual void Visit(BfTypeReference* typeRef);
virtual void Visit(BfNamedTypeReference* typeRef); virtual void Visit(BfNamedTypeReference* typeRef);
virtual void Visit(BfQualifiedTypeReference* qualifiedType); virtual void Visit(BfQualifiedTypeReference* qualifiedType);
@ -2571,17 +2569,6 @@ public:
BfExpression* mExpression; BfExpression* mExpression;
}; BF_AST_DECL(BfAttributedExpression, BfExpression); }; BF_AST_DECL(BfAttributedExpression, BfExpression);
class BfArraySizeSpecifier : public BfAstNode
{
public:
BF_AST_TYPE(BfArraySizeSpecifier, BfAstNode);
BfTokenNode* mOpenToken;
BfTokenNode* mCloseToken;
BfSizedArray<BfExpression*> mArguments;
BfSizedArray<BfTokenNode*> mCommas;
}; BF_AST_DECL(BfArraySizeSpecifier, BfAstNode);
class BfObjectCreateExpression : public BfMethodBoundExpression class BfObjectCreateExpression : public BfMethodBoundExpression
{ {
public: public:

View file

@ -251,18 +251,6 @@ void BfElementVisitor::Visit(BfCollectionInitializerExpression* collectionInitEx
VisitChild(collectionInitExpr->mCloseBrace); VisitChild(collectionInitExpr->mCloseBrace);
} }
void BfElementVisitor::Visit(BfArraySizeSpecifier* arraySizeSpecifier)
{
Visit(arraySizeSpecifier->ToBase());
VisitChild(arraySizeSpecifier->mOpenToken);
for (auto& val : arraySizeSpecifier->mArguments)
VisitChild(val);
for (auto& val : arraySizeSpecifier->mCommas)
VisitChild(val);
VisitChild(arraySizeSpecifier->mCloseToken);
}
void BfElementVisitor::Visit(BfTypeReference* typeRef) void BfElementVisitor::Visit(BfTypeReference* typeRef)
{ {
Visit(typeRef->ToBase()); Visit(typeRef->ToBase());

View file

@ -43,8 +43,7 @@ public:
virtual void Visit(BfBaseExpression* baseExpr); virtual void Visit(BfBaseExpression* baseExpr);
virtual void Visit(BfMixinExpression* thisExpr); virtual void Visit(BfMixinExpression* thisExpr);
virtual void Visit(BfSizedArrayCreateExpression* createExpr); virtual void Visit(BfSizedArrayCreateExpression* createExpr);
virtual void Visit(BfCollectionInitializerExpression* collectionInitExpr); virtual void Visit(BfCollectionInitializerExpression* collectionInitExpr);
virtual void Visit(BfArraySizeSpecifier* arraySizeSpecifier);
virtual void Visit(BfTypeReference* typeRef); virtual void Visit(BfTypeReference* typeRef);
virtual void Visit(BfNamedTypeReference* typeRef); virtual void Visit(BfNamedTypeReference* typeRef);
virtual void Visit(BfQualifiedTypeReference* qualifiedType); virtual void Visit(BfQualifiedTypeReference* qualifiedType);

View file

@ -15530,11 +15530,8 @@ BfTypedValue BfExprEvaluator::SetupNullConditional(BfTypedValue thisValue, BfTok
// Also good // Also good
} }
else else
{ {
if (thisValue.mType->IsStruct()) mModule->Warn(0, StrFormat("Null conditional reference is unnecessary since value type '%s' can never be null", mModule->TypeToString(thisValue.mType).c_str()), dotToken);
mModule->Warn(0, "Struct lookups can never fail, null conditional reference is unnecessary", dotToken);
else
mModule->AssertErrorState();
return thisValue; return thisValue;
} }

View file

@ -7459,7 +7459,7 @@ BfType* BfModule::ResolveTypeRef(BfTypeReference* typeRef, BfPopulateType popula
auto baseDelegateType = ResolveTypeDef(mCompiler->mDelegateTypeDef)->ToTypeInstance(); auto baseDelegateType = ResolveTypeDef(mCompiler->mDelegateTypeDef)->ToTypeInstance();
BfDelegateType* delegateType = new BfDelegateType(); BfDelegateType* delegateType = new BfDelegateType();
Val128 hashContext; Val128 hashContext;
@ -7497,6 +7497,7 @@ BfType* BfModule::ResolveTypeRef(BfTypeReference* typeRef, BfPopulateType popula
delegateType->mDirectAllocNodes.push_back(directTypeRef); delegateType->mDirectAllocNodes.push_back(directTypeRef);
directTypeRef->Init(returnType); directTypeRef->Init(returnType);
methodDef->mReturnTypeRef = directTypeRef; methodDef->mReturnTypeRef = directTypeRef;
delegateType->mReturnType = returnType;
AddDependency(directTypeRef->mType, baseDelegateType, BfDependencyMap::DependencyFlag_ParamOrReturnValue); AddDependency(directTypeRef->mType, baseDelegateType, BfDependencyMap::DependencyFlag_ParamOrReturnValue);
@ -7506,6 +7507,8 @@ BfType* BfModule::ResolveTypeRef(BfTypeReference* typeRef, BfPopulateType popula
for (auto param : delegateTypeRef->mParams) for (auto param : delegateTypeRef->mParams)
{ {
auto paramType = ResolveTypeRef(param->mTypeRef, BfPopulateType_Declaration, BfResolveTypeRefFlag_AllowRef); auto paramType = ResolveTypeRef(param->mTypeRef, BfPopulateType_Declaration, BfResolveTypeRefFlag_AllowRef);
if (paramType == NULL)
paramType = GetPrimitiveType(BfTypeCode_Var);
String paramName; String paramName;
if (param->mNameNode != NULL) if (param->mNameNode != NULL)
paramName = param->mNameNode->ToString(); paramName = param->mNameNode->ToString();
@ -7532,6 +7535,7 @@ BfType* BfModule::ResolveTypeRef(BfTypeReference* typeRef, BfPopulateType popula
methodDef->mParams.push_back(paramDef); methodDef->mParams.push_back(paramDef);
paramIdx++; paramIdx++;
delegateType->mParams.Add(paramType);
AddDependency(paramType, baseDelegateType, BfDependencyMap::DependencyFlag_ParamOrReturnValue); AddDependency(paramType, baseDelegateType, BfDependencyMap::DependencyFlag_ParamOrReturnValue);
} }
@ -7545,7 +7549,7 @@ BfType* BfModule::ResolveTypeRef(BfTypeReference* typeRef, BfPopulateType popula
delegateType->mContext = mContext; delegateType->mContext = mContext;
delegateType->mTypeDef = typeDef; delegateType->mTypeDef = typeDef;
InitType(delegateType, BfPopulateType_DataAndMethods); InitType(delegateType, populateType);
resolvedEntry->mValue = delegateType; resolvedEntry->mValue = delegateType;
// #ifdef _DEBUG // #ifdef _DEBUG
@ -9775,7 +9779,7 @@ void BfModule::DoTypeToString(StringImpl& str, BfType* resolvedType, BfTypeNameF
} }
str += ")"; str += ")";
return; return;
} }
else if (resolvedType->IsDelegateFromTypeRef() || resolvedType->IsFunctionFromTypeRef()) else if (resolvedType->IsDelegateFromTypeRef() || resolvedType->IsFunctionFromTypeRef())
{ {
SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(mCurTypeInstance); SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(mCurTypeInstance);
@ -9795,7 +9799,7 @@ void BfModule::DoTypeToString(StringImpl& str, BfType* resolvedType, BfTypeNameF
str += "delegate "; str += "delegate ";
else else
str += "function "; str += "function ";
DoTypeToString(str, ResolveTypeRef(methodDef->mReturnTypeRef)); DoTypeToString(str, delegateType->mReturnType);
str += "("; str += "(";
for (int paramIdx = 0; paramIdx < methodDef->mParams.size(); paramIdx++) for (int paramIdx = 0; paramIdx < methodDef->mParams.size(); paramIdx++)
{ {
@ -9805,7 +9809,7 @@ void BfModule::DoTypeToString(StringImpl& str, BfType* resolvedType, BfTypeNameF
BfTypeNameFlags innerFlags = (BfTypeNameFlags)(typeNameFlags & ~(BfTypeNameFlag_OmitNamespace | BfTypeNameFlag_OmitOuterType)); BfTypeNameFlags innerFlags = (BfTypeNameFlags)(typeNameFlags & ~(BfTypeNameFlag_OmitNamespace | BfTypeNameFlag_OmitOuterType));
if (delegateType->mIsUnspecializedTypeVariation) if (delegateType->mIsUnspecializedTypeVariation)
innerFlags = (BfTypeNameFlags)(innerFlags & ~BfTypeNameFlag_ResolveGenericParamNames); innerFlags = (BfTypeNameFlags)(innerFlags & ~BfTypeNameFlag_ResolveGenericParamNames);
DoTypeToString(str, ResolveTypeRef(paramDef->mTypeRef), innerFlags, genericMethodNameOverrides); DoTypeToString(str, delegateType->mParams[paramIdx], innerFlags, genericMethodNameOverrides);
str += " "; str += " ";
str += paramDef->mName; str += paramDef->mName;
} }

View file

@ -933,23 +933,6 @@ void BfPrinter::Visit(BfCollectionInitializerExpression* initExpr)
VisitChild(initExpr->mCloseBrace); VisitChild(initExpr->mCloseBrace);
} }
void BfPrinter::Visit(BfArraySizeSpecifier* arraySizeSpecifier)
{
Visit(arraySizeSpecifier->ToBase());
VisitChild(arraySizeSpecifier->mOpenToken);
for (int i = 0; i < (int)arraySizeSpecifier->mArguments.size(); i++)
{
if (i > 0)
{
VisitChild(arraySizeSpecifier->mCommas[i - 1]);
ExpectSpace();
}
VisitChild(arraySizeSpecifier->mArguments[i]);
}
VisitChild(arraySizeSpecifier->mCloseToken);
}
void BfPrinter::Visit(BfTypeReference* typeRef) void BfPrinter::Visit(BfTypeReference* typeRef)
{ {
Visit(typeRef->ToBase()); Visit(typeRef->ToBase());

View file

@ -128,8 +128,7 @@ public:
virtual void Visit(BfBaseExpression* baseExpr) override; virtual void Visit(BfBaseExpression* baseExpr) override;
virtual void Visit(BfMixinExpression* mixinExpr) override; virtual void Visit(BfMixinExpression* mixinExpr) override;
virtual void Visit(BfSizedArrayCreateExpression* createExpr) override; virtual void Visit(BfSizedArrayCreateExpression* createExpr) override;
virtual void Visit(BfCollectionInitializerExpression* initExpr) override; virtual void Visit(BfCollectionInitializerExpression* initExpr) override;
virtual void Visit(BfArraySizeSpecifier* arraySizeSpecifier) override;
virtual void Visit(BfTypeReference* typeRef) override; virtual void Visit(BfTypeReference* typeRef) override;
virtual void Visit(BfNamedTypeReference* typeRef) override; virtual void Visit(BfNamedTypeReference* typeRef) override;
virtual void Visit(BfQualifiedTypeReference* qualifiedType) override; virtual void Visit(BfQualifiedTypeReference* qualifiedType) override;

View file

@ -2072,17 +2072,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;
int hashVal = HASH_DELEGATE; int hashVal = HASH_DELEGATE;
hashVal = ((hashVal ^ (Hash(delegateType->mReturnType, ctx))) << 5) - hashVal;
BfMethodInstance* invokeMethodInstance = ctx->mModule->GetRawMethodInstanceAtIdx(type->ToTypeInstance(), 0, "Invoke"); auto methodDef = delegateType->mTypeDef->mMethods[0];
BF_ASSERT(methodDef->mName == "Invoke");
BF_ASSERT(delegateType->mParams.size() == methodDef->mParams.size());
hashVal = ((hashVal ^ (Hash(invokeMethodInstance->mReturnType, ctx))) << 5) - hashVal; for (int paramIdx = 0; paramIdx < delegateType->mParams.size(); paramIdx++)
for (int paramIdx = 0; paramIdx < invokeMethodInstance->mParams.size(); paramIdx++)
{ {
// Parse attributes? // Parse attributes?
hashVal = ((hashVal ^ (Hash(invokeMethodInstance->GetParamType(paramIdx), ctx))) << 5) - hashVal; hashVal = ((hashVal ^ (Hash(delegateType->mParams[paramIdx], ctx))) << 5) - hashVal;
String paramName = invokeMethodInstance->GetParamName(paramIdx); 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;
} }
@ -2875,18 +2879,19 @@ bool BfResolvedTypeSet::Equals(BfType* lhs, BfType* rhs, LookupContext* ctx)
BfDelegateType* rhsDelegateType = (BfDelegateType*)rhs; BfDelegateType* rhsDelegateType = (BfDelegateType*)rhs;
if (lhsDelegateType->mTypeDef->mIsDelegate != rhsDelegateType->mTypeDef->mIsDelegate) if (lhsDelegateType->mTypeDef->mIsDelegate != rhsDelegateType->mTypeDef->mIsDelegate)
return false; return false;
auto lhsMethodDef = lhsDelegateType->mTypeDef->mMethods[0];
auto rhsMethodDef = rhsDelegateType->mTypeDef->mMethods[0];
BfMethodInstance* lhsInvokeMethodInstance = ctx->mModule->GetRawMethodInstanceAtIdx(lhsDelegateType->ToTypeInstance(), 0, "Invoke"); if (lhsDelegateType->mReturnType != rhsDelegateType->mReturnType)
BfMethodInstance* rhsInvokeMethodInstance = ctx->mModule->GetRawMethodInstanceAtIdx(rhsDelegateType->ToTypeInstance(), 0, "Invoke");
if (lhsInvokeMethodInstance->mReturnType != rhsInvokeMethodInstance->mReturnType)
return false; return false;
if (lhsInvokeMethodInstance->GetParamCount() != rhsInvokeMethodInstance->GetParamCount()) if (lhsDelegateType->mParams.size() != rhsDelegateType->mParams.size())
return false; return false;
for (int paramIdx = 0; paramIdx < lhsInvokeMethodInstance->GetParamCount(); paramIdx++) for (int paramIdx = 0; paramIdx < lhsDelegateType->mParams.size(); paramIdx++)
{ {
if (lhsInvokeMethodInstance->GetParamType(paramIdx) != rhsInvokeMethodInstance->GetParamType(paramIdx)) if (lhsDelegateType->mParams[paramIdx] != rhsDelegateType->mParams[paramIdx])
return false; return false;
if (lhsInvokeMethodInstance->GetParamName(paramIdx) != rhsInvokeMethodInstance->GetParamName(paramIdx)) if (lhsMethodDef->mParams[paramIdx]->mName != rhsMethodDef->mParams[paramIdx]->mName)
return false; return false;
} }
return true; return true;

View file

@ -461,7 +461,7 @@ public:
virtual bool IsString() { return false; } virtual bool IsString() { return false; }
virtual bool IsSizedArray() { return false; } virtual bool IsSizedArray() { return false; }
virtual bool IsUnknownSizedArray() { return false; } virtual bool IsUnknownSizedArray() { return false; }
virtual bool IsArray() { return false; } virtual bool IsArray() { return false; }
virtual bool IsDelegate() { return false; } virtual bool IsDelegate() { return false; }
virtual bool IsFunction() { return false; } virtual bool IsFunction() { return false; }
virtual bool IsDelegateFromTypeRef() { return false; } virtual bool IsDelegateFromTypeRef() { return false; }
@ -1920,16 +1920,20 @@ public:
// These depend on the params in Invoke // 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();
virtual bool IsOnDemand() override { return true; } virtual bool IsOnDemand() override { return true; }
virtual bool IsDelegate() override { return mTypeDef->mIsDelegate; } virtual bool IsDelegate() override { return mTypeDef->mIsDelegate; }
virtual bool IsDelegateFromTypeRef() override { return mTypeDef->mIsDelegate; } virtual bool IsDelegateFromTypeRef() override { return mTypeDef->mIsDelegate; }