From 535045c48aca3e18c4ec7aef95a3f232da871cac Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Sat, 29 Aug 2020 11:28:11 -0700 Subject: [PATCH] Allow method attributes on properties with expression bodies --- IDEHelper/Compiler/BfCompiler.cpp | 5 +++- IDEHelper/Compiler/BfDefBuilder.cpp | 5 +++- IDEHelper/Compiler/BfModule.cpp | 29 +++++++++++++++--------- IDEHelper/Compiler/BfModuleTypeUtils.cpp | 10 ++++---- IDEHelper/Compiler/BfSystem.cpp | 8 +++++++ IDEHelper/Compiler/BfSystem.h | 1 + 6 files changed, 39 insertions(+), 19 deletions(-) diff --git a/IDEHelper/Compiler/BfCompiler.cpp b/IDEHelper/Compiler/BfCompiler.cpp index 9ff718c3..8d1c0dc4 100644 --- a/IDEHelper/Compiler/BfCompiler.cpp +++ b/IDEHelper/Compiler/BfCompiler.cpp @@ -4149,7 +4149,10 @@ void BfCompiler::ProcessAutocompleteTempType() { if ((propDef->mFieldDeclaration != NULL) && (propDef->mFieldDeclaration->mAttributes != NULL)) { - auto customAttrs = module->GetCustomAttributes(propDef->mFieldDeclaration->mAttributes, BfAttributeTargets_Property); + BfAttributeTargets target = BfAttributeTargets_Property; + if (propDef->IsExpressionBodied()) + target = (BfAttributeTargets)(target | BfAttributeTargets_Method); + auto customAttrs = module->GetCustomAttributes(propDef->mFieldDeclaration->mAttributes, target); delete customAttrs; } diff --git a/IDEHelper/Compiler/BfDefBuilder.cpp b/IDEHelper/Compiler/BfDefBuilder.cpp index d88efdab..3ee44108 100644 --- a/IDEHelper/Compiler/BfDefBuilder.cpp +++ b/IDEHelper/Compiler/BfDefBuilder.cpp @@ -949,7 +949,10 @@ void BfDefBuilder::Visit(BfPropertyDeclaration* propertyDeclaration) String methodName; if (auto propExprBody = BfNodeDynCast(propertyDeclaration->mDefinitionBlock)) + { methodName = "get"; + ParseAttributes(propertyDeclaration->mAttributes, methodDef); + } else if ((methodDeclaration != NULL) && (methodDeclaration->mNameNode != NULL)) methodName = methodDeclaration->mNameNode->ToString(); @@ -985,7 +988,7 @@ void BfDefBuilder::Visit(BfPropertyDeclaration* propertyDeclaration) } else { - // Parse had an error, leave this block as an unnamed method + // Parse had an error, leave this block as an unnamed method } methodDef->mBody = methodDeclaration->mBody; diff --git a/IDEHelper/Compiler/BfModule.cpp b/IDEHelper/Compiler/BfModule.cpp index 1a0d627e..9c26e442 100644 --- a/IDEHelper/Compiler/BfModule.cpp +++ b/IDEHelper/Compiler/BfModule.cpp @@ -2884,7 +2884,7 @@ BfError* BfModule::Warn(int warningNum, const StringImpl& warning, BfAstNode* re if (parser != NULL) { int fileLoc = BfFixitFinder::FindLineStartBefore(refNode); - mCompiler->mResolvePassData->mAutoComplete->AddEntry(AutoCompleteEntry("fixit", StrFormat("#unwarn\tunwarn|%s|%d|#unwarn|", parser->mFileName.c_str(), fileLoc).c_str())); + mCompiler->mResolvePassData->mAutoComplete->AddEntry(AutoCompleteEntry("fixit", StrFormat("#unwarn\tunwarn|%s|%d|#unwarn|", parser->mFileName.c_str(), fileLoc).c_str())); if (warningNum != 0) { @@ -19512,20 +19512,27 @@ void BfModule::GetMethodCustomAttributes(BfMethodInstance* methodInstance) SetAndRestoreValue prevTypeInstance(mCurTypeInstance, typeInstance); SetAndRestoreValue prevMethodInstance(mCurMethodInstance, methodInstance); - if ((methodDeclaration != NULL) && (methodDeclaration->mAttributes != NULL)) + BfAttributeTargets attrTarget = ((methodDef->mMethodType == BfMethodType_Ctor) || (methodDef->mMethodType == BfMethodType_CtorCalcAppend)) ? BfAttributeTargets_Constructor : BfAttributeTargets_Method; + BfAttributeDirective* attributeDirective = NULL; + if (methodDeclaration != NULL) + attributeDirective = methodDeclaration->mAttributes; + else if (propertyMethodDeclaration != NULL) + { + attributeDirective = propertyMethodDeclaration->mAttributes; + if (auto exprBody = BfNodeDynCast(propertyMethodDeclaration->mPropertyDeclaration->mDefinitionBlock)) + { + attributeDirective = propertyMethodDeclaration->mPropertyDeclaration->mAttributes; + attrTarget = (BfAttributeTargets)(BfAttributeTargets_Property | BfAttributeTargets_Method); + } + } + + if (attributeDirective != NULL) { if (methodInstance->GetMethodInfoEx()->mMethodCustomAttributes == NULL) methodInstance->mMethodInfoEx->mMethodCustomAttributes = new BfMethodCustomAttributes(); - methodInstance->mMethodInfoEx->mMethodCustomAttributes->mCustomAttributes = GetCustomAttributes(methodDeclaration->mAttributes, - ((methodDef->mMethodType == BfMethodType_Ctor) || (methodDef->mMethodType == BfMethodType_CtorCalcAppend)) ? BfAttributeTargets_Constructor : BfAttributeTargets_Method); + methodInstance->mMethodInfoEx->mMethodCustomAttributes->mCustomAttributes = GetCustomAttributes(attributeDirective, attrTarget); } - else if ((propertyMethodDeclaration != NULL) && (propertyMethodDeclaration->mAttributes != NULL)) - { - if (methodInstance->GetMethodInfoEx()->mMethodCustomAttributes == NULL) - methodInstance->mMethodInfoEx->mMethodCustomAttributes = new BfMethodCustomAttributes(); - methodInstance->mMethodInfoEx->mMethodCustomAttributes->mCustomAttributes = GetCustomAttributes(propertyMethodDeclaration->mAttributes, BfAttributeTargets_Method); - } - + customAttributes = methodInstance->GetCustomAttributes(); if (customAttributes == NULL) { diff --git a/IDEHelper/Compiler/BfModuleTypeUtils.cpp b/IDEHelper/Compiler/BfModuleTypeUtils.cpp index 43e55630..95b1f40b 100644 --- a/IDEHelper/Compiler/BfModuleTypeUtils.cpp +++ b/IDEHelper/Compiler/BfModuleTypeUtils.cpp @@ -2887,15 +2887,13 @@ void BfModule::DoPopulateType(BfType* resolvedTypeRef, BfPopulateType populateTy typeState.mTypeInstance = typeInstance; SetAndRestoreValue prevTypeState(mContext->mCurTypeState, &typeState); - if (propDef->mFieldDeclaration->mAttributes != NULL) - { - auto customAttrs = GetCustomAttributes(propDef->mFieldDeclaration->mAttributes, BfAttributeTargets_Property); - delete customAttrs; - } + BfAttributeTargets target = BfAttributeTargets_Property; + if (propDef->IsExpressionBodied()) + target = (BfAttributeTargets)(target | BfAttributeTargets_Method); if (propDef->mFieldDeclaration->mAttributes != NULL) { - auto customAttrs = GetCustomAttributes(propDef->mFieldDeclaration->mAttributes, BfAttributeTargets_Property); + auto customAttrs = GetCustomAttributes(propDef->mFieldDeclaration->mAttributes, target); delete customAttrs; } diff --git a/IDEHelper/Compiler/BfSystem.cpp b/IDEHelper/Compiler/BfSystem.cpp index 7d994d17..6c1e5a8b 100644 --- a/IDEHelper/Compiler/BfSystem.cpp +++ b/IDEHelper/Compiler/BfSystem.cpp @@ -398,6 +398,14 @@ bool BfPropertyDef::HasExplicitInterface() return false; } +bool BfPropertyDef::IsExpressionBodied() +{ + auto propertyDeclaration = (BfPropertyDeclaration*)mFieldDeclaration; + if (auto expr = BfNodeDynCast(propertyDeclaration->mDefinitionBlock)) + return true; + return false; +} + BfAstNode * BfPropertyDef::GetRefNode() { BfPropertyDeclaration* propDecl = (BfPropertyDeclaration*)mFieldDeclaration; diff --git a/IDEHelper/Compiler/BfSystem.h b/IDEHelper/Compiler/BfSystem.h index ab075d0c..6650c920 100644 --- a/IDEHelper/Compiler/BfSystem.h +++ b/IDEHelper/Compiler/BfSystem.h @@ -580,6 +580,7 @@ public: bool IsVirtual(); bool HasExplicitInterface(); + bool IsExpressionBodied(); BfAstNode* GetRefNode(); };