From 95e4c1b3ca142a0a35ab44ce2f525ab4573a5faa Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Thu, 23 Jan 2020 12:02:54 -0800 Subject: [PATCH] Fixed repr for on-demand delegate types, avoiding circular Event refs --- IDEHelper/Compiler/BfAst.cpp | 5 ---- IDEHelper/Compiler/BfAst.h | 15 +---------- IDEHelper/Compiler/BfElementVisitor.cpp | 12 --------- IDEHelper/Compiler/BfElementVisitor.h | 3 +-- IDEHelper/Compiler/BfExprEvaluator.cpp | 7 ++--- IDEHelper/Compiler/BfModuleTypeUtils.cpp | 14 ++++++---- IDEHelper/Compiler/BfPrinter.cpp | 17 ------------ IDEHelper/Compiler/BfPrinter.h | 3 +-- IDEHelper/Compiler/BfResolvedTypeUtils.cpp | 31 +++++++++++++--------- IDEHelper/Compiler/BfResolvedTypeUtils.h | 10 ++++--- 10 files changed, 39 insertions(+), 78 deletions(-) diff --git a/IDEHelper/Compiler/BfAst.cpp b/IDEHelper/Compiler/BfAst.cpp index 1dd0b7b6..01a324e6 100644 --- a/IDEHelper/Compiler/BfAst.cpp +++ b/IDEHelper/Compiler/BfAst.cpp @@ -196,11 +196,6 @@ void BfStructuralVisitor::Visit(BfCollectionInitializerExpression* collectionIni Visit(collectionInitExpr->ToBase()); } -void BfStructuralVisitor::Visit(BfArraySizeSpecifier* arraySizeSpecifier) -{ - Visit(arraySizeSpecifier->ToBase()); -} - void BfStructuralVisitor::Visit(BfTypeReference* typeRef) { Visit(typeRef->ToBase()); diff --git a/IDEHelper/Compiler/BfAst.h b/IDEHelper/Compiler/BfAst.h index 5b6572da..4fd8660a 100644 --- a/IDEHelper/Compiler/BfAst.h +++ b/IDEHelper/Compiler/BfAst.h @@ -351,7 +351,6 @@ class BfDefaultExpression; class BfUninitializedExpression; class BfConditionalExpression; class BfCollectionInitializerExpression; -class BfArraySizeSpecifier; class BfSizedArrayCreateExpression; class BfEmptyStatement; class BfGenericOperatorConstraint; @@ -432,8 +431,7 @@ public: virtual void Visit(BfBaseExpression* baseExpr); virtual void Visit(BfMixinExpression* thisExpr); virtual void Visit(BfSizedArrayCreateExpression* createExpr); - virtual void Visit(BfCollectionInitializerExpression* collectionInitExpr); - virtual void Visit(BfArraySizeSpecifier* arraySizeSpecifier); + virtual void Visit(BfCollectionInitializerExpression* collectionInitExpr); virtual void Visit(BfTypeReference* typeRef); virtual void Visit(BfNamedTypeReference* typeRef); virtual void Visit(BfQualifiedTypeReference* qualifiedType); @@ -2571,17 +2569,6 @@ public: BfExpression* mExpression; }; BF_AST_DECL(BfAttributedExpression, BfExpression); -class BfArraySizeSpecifier : public BfAstNode -{ -public: - BF_AST_TYPE(BfArraySizeSpecifier, BfAstNode); - - BfTokenNode* mOpenToken; - BfTokenNode* mCloseToken; - BfSizedArray mArguments; - BfSizedArray mCommas; -}; BF_AST_DECL(BfArraySizeSpecifier, BfAstNode); - class BfObjectCreateExpression : public BfMethodBoundExpression { public: diff --git a/IDEHelper/Compiler/BfElementVisitor.cpp b/IDEHelper/Compiler/BfElementVisitor.cpp index 96c2516c..04ea2a49 100644 --- a/IDEHelper/Compiler/BfElementVisitor.cpp +++ b/IDEHelper/Compiler/BfElementVisitor.cpp @@ -251,18 +251,6 @@ void BfElementVisitor::Visit(BfCollectionInitializerExpression* collectionInitEx 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) { Visit(typeRef->ToBase()); diff --git a/IDEHelper/Compiler/BfElementVisitor.h b/IDEHelper/Compiler/BfElementVisitor.h index d14d81d8..c7c9d74d 100644 --- a/IDEHelper/Compiler/BfElementVisitor.h +++ b/IDEHelper/Compiler/BfElementVisitor.h @@ -43,8 +43,7 @@ public: virtual void Visit(BfBaseExpression* baseExpr); virtual void Visit(BfMixinExpression* thisExpr); virtual void Visit(BfSizedArrayCreateExpression* createExpr); - virtual void Visit(BfCollectionInitializerExpression* collectionInitExpr); - virtual void Visit(BfArraySizeSpecifier* arraySizeSpecifier); + virtual void Visit(BfCollectionInitializerExpression* collectionInitExpr); virtual void Visit(BfTypeReference* typeRef); virtual void Visit(BfNamedTypeReference* typeRef); virtual void Visit(BfQualifiedTypeReference* qualifiedType); diff --git a/IDEHelper/Compiler/BfExprEvaluator.cpp b/IDEHelper/Compiler/BfExprEvaluator.cpp index 692720b6..cd26017a 100644 --- a/IDEHelper/Compiler/BfExprEvaluator.cpp +++ b/IDEHelper/Compiler/BfExprEvaluator.cpp @@ -15530,11 +15530,8 @@ BfTypedValue BfExprEvaluator::SetupNullConditional(BfTypedValue thisValue, BfTok // Also good } else - { - if (thisValue.mType->IsStruct()) - mModule->Warn(0, "Struct lookups can never fail, null conditional reference is unnecessary", dotToken); - else - mModule->AssertErrorState(); + { + mModule->Warn(0, StrFormat("Null conditional reference is unnecessary since value type '%s' can never be null", mModule->TypeToString(thisValue.mType).c_str()), dotToken); return thisValue; } diff --git a/IDEHelper/Compiler/BfModuleTypeUtils.cpp b/IDEHelper/Compiler/BfModuleTypeUtils.cpp index c11ad752..287584b9 100644 --- a/IDEHelper/Compiler/BfModuleTypeUtils.cpp +++ b/IDEHelper/Compiler/BfModuleTypeUtils.cpp @@ -7459,7 +7459,7 @@ BfType* BfModule::ResolveTypeRef(BfTypeReference* typeRef, BfPopulateType popula auto baseDelegateType = ResolveTypeDef(mCompiler->mDelegateTypeDef)->ToTypeInstance(); - BfDelegateType* delegateType = new BfDelegateType(); + BfDelegateType* delegateType = new BfDelegateType(); Val128 hashContext; @@ -7497,6 +7497,7 @@ BfType* BfModule::ResolveTypeRef(BfTypeReference* typeRef, BfPopulateType popula delegateType->mDirectAllocNodes.push_back(directTypeRef); directTypeRef->Init(returnType); methodDef->mReturnTypeRef = directTypeRef; + delegateType->mReturnType = returnType; AddDependency(directTypeRef->mType, baseDelegateType, BfDependencyMap::DependencyFlag_ParamOrReturnValue); @@ -7506,6 +7507,8 @@ BfType* BfModule::ResolveTypeRef(BfTypeReference* typeRef, BfPopulateType popula for (auto param : delegateTypeRef->mParams) { auto paramType = ResolveTypeRef(param->mTypeRef, BfPopulateType_Declaration, BfResolveTypeRefFlag_AllowRef); + if (paramType == NULL) + paramType = GetPrimitiveType(BfTypeCode_Var); String paramName; if (param->mNameNode != NULL) paramName = param->mNameNode->ToString(); @@ -7532,6 +7535,7 @@ BfType* BfModule::ResolveTypeRef(BfTypeReference* typeRef, BfPopulateType popula methodDef->mParams.push_back(paramDef); paramIdx++; + delegateType->mParams.Add(paramType); AddDependency(paramType, baseDelegateType, BfDependencyMap::DependencyFlag_ParamOrReturnValue); } @@ -7545,7 +7549,7 @@ BfType* BfModule::ResolveTypeRef(BfTypeReference* typeRef, BfPopulateType popula delegateType->mContext = mContext; delegateType->mTypeDef = typeDef; - InitType(delegateType, BfPopulateType_DataAndMethods); + InitType(delegateType, populateType); resolvedEntry->mValue = delegateType; // #ifdef _DEBUG @@ -9775,7 +9779,7 @@ void BfModule::DoTypeToString(StringImpl& str, BfType* resolvedType, BfTypeNameF } str += ")"; return; - } + } else if (resolvedType->IsDelegateFromTypeRef() || resolvedType->IsFunctionFromTypeRef()) { SetAndRestoreValue prevTypeInstance(mCurTypeInstance); @@ -9795,7 +9799,7 @@ void BfModule::DoTypeToString(StringImpl& str, BfType* resolvedType, BfTypeNameF str += "delegate "; else str += "function "; - DoTypeToString(str, ResolveTypeRef(methodDef->mReturnTypeRef)); + DoTypeToString(str, delegateType->mReturnType); str += "("; 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)); if (delegateType->mIsUnspecializedTypeVariation) innerFlags = (BfTypeNameFlags)(innerFlags & ~BfTypeNameFlag_ResolveGenericParamNames); - DoTypeToString(str, ResolveTypeRef(paramDef->mTypeRef), innerFlags, genericMethodNameOverrides); + DoTypeToString(str, delegateType->mParams[paramIdx], innerFlags, genericMethodNameOverrides); str += " "; str += paramDef->mName; } diff --git a/IDEHelper/Compiler/BfPrinter.cpp b/IDEHelper/Compiler/BfPrinter.cpp index 255d1ee6..3fac32e1 100644 --- a/IDEHelper/Compiler/BfPrinter.cpp +++ b/IDEHelper/Compiler/BfPrinter.cpp @@ -933,23 +933,6 @@ void BfPrinter::Visit(BfCollectionInitializerExpression* initExpr) 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) { Visit(typeRef->ToBase()); diff --git a/IDEHelper/Compiler/BfPrinter.h b/IDEHelper/Compiler/BfPrinter.h index f7c736bb..6e755161 100644 --- a/IDEHelper/Compiler/BfPrinter.h +++ b/IDEHelper/Compiler/BfPrinter.h @@ -128,8 +128,7 @@ public: virtual void Visit(BfBaseExpression* baseExpr) override; virtual void Visit(BfMixinExpression* mixinExpr) override; virtual void Visit(BfSizedArrayCreateExpression* createExpr) override; - virtual void Visit(BfCollectionInitializerExpression* initExpr) override; - virtual void Visit(BfArraySizeSpecifier* arraySizeSpecifier) override; + virtual void Visit(BfCollectionInitializerExpression* initExpr) override; virtual void Visit(BfTypeReference* typeRef) override; virtual void Visit(BfNamedTypeReference* typeRef) override; virtual void Visit(BfQualifiedTypeReference* qualifiedType) override; diff --git a/IDEHelper/Compiler/BfResolvedTypeUtils.cpp b/IDEHelper/Compiler/BfResolvedTypeUtils.cpp index 2661f6f4..141b66b7 100644 --- a/IDEHelper/Compiler/BfResolvedTypeUtils.cpp +++ b/IDEHelper/Compiler/BfResolvedTypeUtils.cpp @@ -2072,17 +2072,21 @@ int BfResolvedTypeSet::Hash(BfType* type, LookupContext* ctx, bool allowRef) } else if (type->IsDelegateFromTypeRef() || type->IsFunctionFromTypeRef()) { + auto delegateType = (BfDelegateType*)type; + 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 < invokeMethodInstance->mParams.size(); paramIdx++) + for (int paramIdx = 0; paramIdx < delegateType->mParams.size(); paramIdx++) { // Parse attributes? - hashVal = ((hashVal ^ (Hash(invokeMethodInstance->GetParamType(paramIdx), ctx))) << 5) - hashVal; - String paramName = invokeMethodInstance->GetParamName(paramIdx); + hashVal = ((hashVal ^ (Hash(delegateType->mParams[paramIdx], ctx))) << 5) - hashVal; + String paramName = methodDef->mParams[paramIdx]->mName; int nameHash = (int)Hash64(paramName.c_str(), (int)paramName.length()); hashVal = ((hashVal ^ (nameHash)) << 5) - hashVal; } @@ -2875,18 +2879,19 @@ bool BfResolvedTypeSet::Equals(BfType* lhs, BfType* rhs, LookupContext* ctx) BfDelegateType* rhsDelegateType = (BfDelegateType*)rhs; if (lhsDelegateType->mTypeDef->mIsDelegate != rhsDelegateType->mTypeDef->mIsDelegate) return false; + + auto lhsMethodDef = lhsDelegateType->mTypeDef->mMethods[0]; + auto rhsMethodDef = rhsDelegateType->mTypeDef->mMethods[0]; - BfMethodInstance* lhsInvokeMethodInstance = ctx->mModule->GetRawMethodInstanceAtIdx(lhsDelegateType->ToTypeInstance(), 0, "Invoke"); - BfMethodInstance* rhsInvokeMethodInstance = ctx->mModule->GetRawMethodInstanceAtIdx(rhsDelegateType->ToTypeInstance(), 0, "Invoke"); - if (lhsInvokeMethodInstance->mReturnType != rhsInvokeMethodInstance->mReturnType) + if (lhsDelegateType->mReturnType != rhsDelegateType->mReturnType) return false; - if (lhsInvokeMethodInstance->GetParamCount() != rhsInvokeMethodInstance->GetParamCount()) + if (lhsDelegateType->mParams.size() != rhsDelegateType->mParams.size()) 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; - if (lhsInvokeMethodInstance->GetParamName(paramIdx) != rhsInvokeMethodInstance->GetParamName(paramIdx)) + if (lhsMethodDef->mParams[paramIdx]->mName != rhsMethodDef->mParams[paramIdx]->mName) return false; } return true; diff --git a/IDEHelper/Compiler/BfResolvedTypeUtils.h b/IDEHelper/Compiler/BfResolvedTypeUtils.h index 289af43b..3fd3977a 100644 --- a/IDEHelper/Compiler/BfResolvedTypeUtils.h +++ b/IDEHelper/Compiler/BfResolvedTypeUtils.h @@ -461,7 +461,7 @@ public: virtual bool IsString() { return false; } virtual bool IsSizedArray() { return false; } virtual bool IsUnknownSizedArray() { return false; } - virtual bool IsArray() { return false; } + virtual bool IsArray() { return false; } virtual bool IsDelegate() { return false; } virtual bool IsFunction() { return false; } virtual bool IsDelegateFromTypeRef() { return false; } @@ -1920,16 +1920,20 @@ public: // These depend on the params in Invoke bool mIsUnspecializedType; bool mIsUnspecializedTypeVariation; + + BfType* mReturnType; + Array mParams; public: BfDelegateType() { mIsUnspecializedType = false; mIsUnspecializedTypeVariation = false; + mReturnType = NULL; } ~BfDelegateType(); - - virtual bool IsOnDemand() override { return true; } + + virtual bool IsOnDemand() override { return true; } virtual bool IsDelegate() override { return mTypeDef->mIsDelegate; } virtual bool IsDelegateFromTypeRef() override { return mTypeDef->mIsDelegate; }