From 86d8f78761b00184cbf46234377429fb2147b298 Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Mon, 18 May 2020 13:12:18 -0700 Subject: [PATCH] Allowed more flexible attribute type lookup, supporting inner types --- IDEHelper/Compiler/BfCompiler.cpp | 44 +++++++++++------------- IDEHelper/Compiler/BfExprEvaluator.cpp | 6 ++-- IDEHelper/Compiler/BfModule.cpp | 44 +++++------------------- IDEHelper/Compiler/BfModule.h | 3 +- IDEHelper/Compiler/BfModuleTypeUtils.cpp | 18 +++++++--- 5 files changed, 49 insertions(+), 66 deletions(-) diff --git a/IDEHelper/Compiler/BfCompiler.cpp b/IDEHelper/Compiler/BfCompiler.cpp index 49226803..7a6c136c 100644 --- a/IDEHelper/Compiler/BfCompiler.cpp +++ b/IDEHelper/Compiler/BfCompiler.cpp @@ -4282,37 +4282,33 @@ void BfCompiler::GetSymbolReferences() while (attrib != NULL) { if (attrib->mAttributeTypeRef != NULL) - { - String attrName = attrib->mAttributeTypeRef->ToString(); - BfType* attrType = NULL; - - BfAtomComposite nameComposite; - if (mSystem->ParseAtomComposite(attrName + "Attribute", nameComposite)) + { + auto attrType = module->ResolveTypeRef(attrib->mAttributeTypeRef, BfPopulateType_Identity, BfResolveTypeRefFlag_Attribute); + BfTypeDef* attrTypeDef = NULL; + if ((attrType != NULL) && (attrType->IsTypeInstance())) + attrTypeDef = attrType->ToTypeInstance()->mTypeDef; + if (attrTypeDef != NULL) { - BfTypeDef* attrTypeDef = module->FindTypeDefRaw(nameComposite, 0, replaceTypeInst, declaringType, NULL); - if (attrTypeDef != NULL) - { - mResolvePassData->HandleTypeReference(attrib->mAttributeTypeRef, attrTypeDef); + mResolvePassData->HandleTypeReference(attrib->mAttributeTypeRef, attrTypeDef); - attrTypeDef->PopulateMemberSets(); - for (auto argExpr : attrib->mArguments) + attrTypeDef->PopulateMemberSets(); + for (auto argExpr : attrib->mArguments) + { + if (auto assignExpr = BfNodeDynCast(argExpr)) { - if (auto assignExpr = BfNodeDynCast(argExpr)) + auto propName = assignExpr->mLeft->ToString(); + BfMemberSetEntry* propDefEntry; + if (attrTypeDef->mPropertySet.TryGetWith(propName, &propDefEntry)) { - auto propName = assignExpr->mLeft->ToString(); - BfMemberSetEntry* propDefEntry; - if (attrTypeDef->mPropertySet.TryGetWith(propName, &propDefEntry)) - { - mResolvePassData->HandlePropertyReference(assignExpr->mLeft, attrTypeDef, (BfPropertyDef*)propDefEntry->mMemberDef); - } - else if (attrTypeDef->mFieldSet.TryGetWith(propName, &propDefEntry)) - { - mResolvePassData->HandleFieldReference(assignExpr->mLeft, attrTypeDef, (BfFieldDef*)propDefEntry->mMemberDef); - } + mResolvePassData->HandlePropertyReference(assignExpr->mLeft, attrTypeDef, (BfPropertyDef*)propDefEntry->mMemberDef); + } + else if (attrTypeDef->mFieldSet.TryGetWith(propName, &propDefEntry)) + { + mResolvePassData->HandleFieldReference(assignExpr->mLeft, attrTypeDef, (BfFieldDef*)propDefEntry->mMemberDef); } } } - } + } } attrib = attrib->mNextAttribute; diff --git a/IDEHelper/Compiler/BfExprEvaluator.cpp b/IDEHelper/Compiler/BfExprEvaluator.cpp index 1ba4a6eb..d10f1d22 100644 --- a/IDEHelper/Compiler/BfExprEvaluator.cpp +++ b/IDEHelper/Compiler/BfExprEvaluator.cpp @@ -5062,8 +5062,10 @@ BfTypedValue BfExprEvaluator::CreateCall(BfAstNode* targetSrc, const BfTypedValu } // Temporarily disable so we don't capture calls in params - SetAndRestoreValue prevBindResult(mFunctionBindResult, NULL); - SetAndRestoreValue prevAllowVariableDeclarations(mModule->mCurMethodState->mCurScope->mAllowVariableDeclarations, false); // Don't allow variable declarations in arguments + SetAndRestoreValue prevBindResult(mFunctionBindResult, NULL); + SetAndRestoreValue prevAllowVariableDeclarations; + if (mModule->mCurMethodState != NULL) + prevAllowVariableDeclarations.Init(mModule->mCurMethodState->mCurScope->mAllowVariableDeclarations, false); BfMethodInstance* methodInstance = moduleMethodInstance.mMethodInstance; diff --git a/IDEHelper/Compiler/BfModule.cpp b/IDEHelper/Compiler/BfModule.cpp index c9eedba0..47055993 100644 --- a/IDEHelper/Compiler/BfModule.cpp +++ b/IDEHelper/Compiler/BfModule.cpp @@ -9791,17 +9791,11 @@ void BfModule::GetCustomAttributes(BfCustomAttributes* customAttributes, BfAttri continue; } - String attrName = attributesDirective->mAttributeTypeRef->ToString(); - BfType* attrType = NULL; - BfAtomComposite nameComposite; - BfTypeDef* attrTypeDef = NULL; - if (mSystem->ParseAtomComposite(attrName + "Attribute", nameComposite)) - { - BfTypeLookupError error; - error.mRefNode = attributesDirective->mAttributeTypeRef; - attrTypeDef = FindTypeDefRaw(nameComposite, 0, mCurTypeInstance, GetActiveTypeDef(NULL, true), &error); - } - + BfType* attrType = ResolveTypeRef(attributesDirective->mAttributeTypeRef, BfPopulateType_Identity, BfResolveTypeRefFlag_Attribute); + BfTypeDef* attrTypeDef = NULL; + if ((attrType != NULL) && (attrType->IsTypeInstance())) + attrTypeDef = attrType->ToTypeInstance()->mTypeDef; + if ((mCompiler->mResolvePassData != NULL) && (mCompiler->mResolvePassData->mAutoComplete != NULL)) { mCompiler->mResolvePassData->mAutoComplete->CheckAttributeTypeRef(attributesDirective->mAttributeTypeRef); @@ -9809,26 +9803,17 @@ void BfModule::GetCustomAttributes(BfCustomAttributes* customAttributes, BfAttri mCompiler->mResolvePassData->HandleTypeReference(attributesDirective->mAttributeTypeRef, attrTypeDef); } - if (attrTypeDef == NULL) - { - TypeRefNotFound(attributesDirective->mAttributeTypeRef, "Attribute"); - continue; - } - bool isBypassedAttr = false; if (attrTypeDef != NULL) - { - attrType = mContext->mUnreifiedModule->ResolveTypeDef(attrTypeDef, BfPopulateType_Identity); - + { // 'Object' has some dependencies on some attributes, but those attributes are classes so we have a circular dependency issue // We solve it by having a 'bypass' for known attributes that Object depends on if ((attributesDirective->mArguments.empty()) && (autoComplete == NULL) && (attrType != NULL) && (attrType->IsTypeInstance())) { - if (attrTypeDef == mCompiler->mCReprAttributeTypeDef) - { - BfTypeInstance* attrTypeInst = attrType->ToTypeInstance(); - + //if (attrTypeDef == mCompiler->mCReprAttributeTypeDef) + if (attrType->IsInstanceOf(mCompiler->mCReprAttributeTypeDef)) + { for (auto methodDef : attrTypeDef->mMethods) { if ((methodDef->mMethodType == BfMethodType_Ctor) && (methodDef->mProtection == BfProtection_Public)) @@ -9860,17 +9845,6 @@ void BfModule::GetCustomAttributes(BfCustomAttributes* customAttributes, BfAttri } } - // Don't allow this - /*else if (mContext->mCurTypeState != NULL) - { - SetAndRestoreValue prevTypeRef(mContext->mCurTypeState->mCurAttributeTypeRef, attributesDirective->mAttributeTypeRef); - attrType = mContext->mUnreifiedModule->ResolveTypeRef(attributesDirective->mAttributeTypeRef, BfPopulateType_DataAndMethods); - } - else - { - attrType = mContext->mUnreifiedModule->ResolveTypeRef(attributesDirective->mAttributeTypeRef); - }*/ - BfTypeInstance* attrTypeInst = NULL; if (attrType == NULL) continue; diff --git a/IDEHelper/Compiler/BfModule.h b/IDEHelper/Compiler/BfModule.h index cf8c3ae8..0c722a4b 100644 --- a/IDEHelper/Compiler/BfModule.h +++ b/IDEHelper/Compiler/BfModule.h @@ -1276,6 +1276,7 @@ enum BfResolveTypeRefFlags BfResolveTypeRefFlag_AllowGenericParamConstValue = 0x10 | 0x20, BfResolveTypeRefFlag_AutoComplete = 0x40, BfResolveTypeRefFlag_FromIndirectSource = 0x80, // Such as a type alias or a generic parameter + BfResolveTypeRefFlag_Attribute = 0x100 }; enum BfSrcPosFlags @@ -1689,7 +1690,7 @@ public: BfTypeDef* FindTypeDefRaw(const BfAtomComposite& findName, int numGenericArgs, BfTypeInstance* typeInstance, BfTypeDef* useTypeDef, BfTypeLookupError* error); BfTypeDef* FindTypeDef(const BfAtomComposite& findName, int numGenericArgs = 0, BfTypeInstance* typeInstanceOverride = NULL, BfTypeLookupError* error = NULL); BfTypeDef* FindTypeDef(const StringImpl& typeName, int numGenericArgs = 0, BfTypeInstance* typeInstanceOverride = NULL, BfTypeLookupError* error = NULL); - BfTypeDef* FindTypeDef(BfTypeReference* typeRef, BfTypeInstance* typeInstanceOverride = NULL, BfTypeLookupError* error = NULL, int numGenericParams = 0); + BfTypeDef* FindTypeDef(BfTypeReference* typeRef, BfTypeInstance* typeInstanceOverride = NULL, BfTypeLookupError* error = NULL, int numGenericParams = 0, BfResolveTypeRefFlags resolveFlags = (BfResolveTypeRefFlags)0); BfTypedValue TryLookupGenericConstVaue(BfIdentifierNode* identifierNode, BfType* expectingType); void CheckTypeRefFixit(BfAstNode* typeRef, const char* appendName = NULL); void CheckIdentifierFixit(BfAstNode* node); diff --git a/IDEHelper/Compiler/BfModuleTypeUtils.cpp b/IDEHelper/Compiler/BfModuleTypeUtils.cpp index 3ee2308d..00e66dbe 100644 --- a/IDEHelper/Compiler/BfModuleTypeUtils.cpp +++ b/IDEHelper/Compiler/BfModuleTypeUtils.cpp @@ -6398,7 +6398,7 @@ BfTypeDef* BfModule::FindTypeDef(const StringImpl& typeName, int numGenericArgs, return result; } -BfTypeDef* BfModule::FindTypeDef(BfTypeReference* typeRef, BfTypeInstance* typeInstanceOverride, BfTypeLookupError* error, int numGenericParams) +BfTypeDef* BfModule::FindTypeDef(BfTypeReference* typeRef, BfTypeInstance* typeInstanceOverride, BfTypeLookupError* error, int numGenericParams, BfResolveTypeRefFlags resolveFlags) { BP_ZONE("BfModule::FindTypeDef_5"); @@ -6442,10 +6442,20 @@ BfTypeDef* BfModule::FindTypeDef(BfTypeReference* typeRef, BfTypeInstance* typeI } BfSizedAtomComposite findName; - if (!mSystem->ParseAtomComposite(findNameStr, findName)) + if ((resolveFlags & BfResolveTypeRefFlag_Attribute) != 0) { - return NULL; + String attributeName; + attributeName += findNameStr; + attributeName += "Attribute"; + if (!mSystem->ParseAtomComposite(attributeName, findName)) + return NULL; } + else + { + if (!mSystem->ParseAtomComposite(findNameStr, findName)) + return NULL; + } + #ifdef BF_AST_HAS_PARENT_MEMBER if (auto parentGenericTypeRef = BfNodeDynCast(typeRef->mParent)) @@ -6972,7 +6982,7 @@ BfType* BfModule::ResolveTypeRef(BfTypeReference* typeRef, BfPopulateType popula { BfTypeLookupError error; error.mRefNode = typeRef; - typeDef = FindTypeDef(typeRef, contextTypeInstance, &error); + typeDef = FindTypeDef(typeRef, contextTypeInstance, &error, 0, resolveFlags); if (auto namedTypeRef = BfNodeDynCast(typeRef)) {