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

Allow method attributes on properties with expression bodies

This commit is contained in:
Brian Fiete 2020-08-29 11:28:11 -07:00
parent 4369e07a55
commit 535045c48a
6 changed files with 39 additions and 19 deletions

View file

@ -4149,7 +4149,10 @@ void BfCompiler::ProcessAutocompleteTempType()
{ {
if ((propDef->mFieldDeclaration != NULL) && (propDef->mFieldDeclaration->mAttributes != NULL)) 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; delete customAttrs;
} }

View file

@ -949,7 +949,10 @@ void BfDefBuilder::Visit(BfPropertyDeclaration* propertyDeclaration)
String methodName; String methodName;
if (auto propExprBody = BfNodeDynCast<BfPropertyBodyExpression>(propertyDeclaration->mDefinitionBlock)) if (auto propExprBody = BfNodeDynCast<BfPropertyBodyExpression>(propertyDeclaration->mDefinitionBlock))
{
methodName = "get"; methodName = "get";
ParseAttributes(propertyDeclaration->mAttributes, methodDef);
}
else if ((methodDeclaration != NULL) && (methodDeclaration->mNameNode != NULL)) else if ((methodDeclaration != NULL) && (methodDeclaration->mNameNode != NULL))
methodName = methodDeclaration->mNameNode->ToString(); methodName = methodDeclaration->mNameNode->ToString();
@ -985,7 +988,7 @@ void BfDefBuilder::Visit(BfPropertyDeclaration* propertyDeclaration)
} }
else 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; methodDef->mBody = methodDeclaration->mBody;

View file

@ -2884,7 +2884,7 @@ BfError* BfModule::Warn(int warningNum, const StringImpl& warning, BfAstNode* re
if (parser != NULL) if (parser != NULL)
{ {
int fileLoc = BfFixitFinder::FindLineStartBefore(refNode); 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) if (warningNum != 0)
{ {
@ -19512,20 +19512,27 @@ void BfModule::GetMethodCustomAttributes(BfMethodInstance* methodInstance)
SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(mCurTypeInstance, typeInstance); SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(mCurTypeInstance, typeInstance);
SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(mCurMethodInstance, methodInstance); SetAndRestoreValue<BfMethodInstance*> 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<BfPropertyBodyExpression>(propertyMethodDeclaration->mPropertyDeclaration->mDefinitionBlock))
{
attributeDirective = propertyMethodDeclaration->mPropertyDeclaration->mAttributes;
attrTarget = (BfAttributeTargets)(BfAttributeTargets_Property | BfAttributeTargets_Method);
}
}
if (attributeDirective != NULL)
{ {
if (methodInstance->GetMethodInfoEx()->mMethodCustomAttributes == NULL) if (methodInstance->GetMethodInfoEx()->mMethodCustomAttributes == NULL)
methodInstance->mMethodInfoEx->mMethodCustomAttributes = new BfMethodCustomAttributes(); methodInstance->mMethodInfoEx->mMethodCustomAttributes = new BfMethodCustomAttributes();
methodInstance->mMethodInfoEx->mMethodCustomAttributes->mCustomAttributes = GetCustomAttributes(methodDeclaration->mAttributes, methodInstance->mMethodInfoEx->mMethodCustomAttributes->mCustomAttributes = GetCustomAttributes(attributeDirective, attrTarget);
((methodDef->mMethodType == BfMethodType_Ctor) || (methodDef->mMethodType == BfMethodType_CtorCalcAppend)) ? BfAttributeTargets_Constructor : BfAttributeTargets_Method);
} }
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(); customAttributes = methodInstance->GetCustomAttributes();
if (customAttributes == NULL) if (customAttributes == NULL)
{ {

View file

@ -2887,15 +2887,13 @@ void BfModule::DoPopulateType(BfType* resolvedTypeRef, BfPopulateType populateTy
typeState.mTypeInstance = typeInstance; typeState.mTypeInstance = typeInstance;
SetAndRestoreValue<BfTypeState*> prevTypeState(mContext->mCurTypeState, &typeState); SetAndRestoreValue<BfTypeState*> prevTypeState(mContext->mCurTypeState, &typeState);
if (propDef->mFieldDeclaration->mAttributes != NULL) BfAttributeTargets target = BfAttributeTargets_Property;
{ if (propDef->IsExpressionBodied())
auto customAttrs = GetCustomAttributes(propDef->mFieldDeclaration->mAttributes, BfAttributeTargets_Property); target = (BfAttributeTargets)(target | BfAttributeTargets_Method);
delete customAttrs;
}
if (propDef->mFieldDeclaration->mAttributes != NULL) if (propDef->mFieldDeclaration->mAttributes != NULL)
{ {
auto customAttrs = GetCustomAttributes(propDef->mFieldDeclaration->mAttributes, BfAttributeTargets_Property); auto customAttrs = GetCustomAttributes(propDef->mFieldDeclaration->mAttributes, target);
delete customAttrs; delete customAttrs;
} }

View file

@ -398,6 +398,14 @@ bool BfPropertyDef::HasExplicitInterface()
return false; return false;
} }
bool BfPropertyDef::IsExpressionBodied()
{
auto propertyDeclaration = (BfPropertyDeclaration*)mFieldDeclaration;
if (auto expr = BfNodeDynCast<BfPropertyBodyExpression>(propertyDeclaration->mDefinitionBlock))
return true;
return false;
}
BfAstNode * BfPropertyDef::GetRefNode() BfAstNode * BfPropertyDef::GetRefNode()
{ {
BfPropertyDeclaration* propDecl = (BfPropertyDeclaration*)mFieldDeclaration; BfPropertyDeclaration* propDecl = (BfPropertyDeclaration*)mFieldDeclaration;

View file

@ -580,6 +580,7 @@ public:
bool IsVirtual(); bool IsVirtual();
bool HasExplicitInterface(); bool HasExplicitInterface();
bool IsExpressionBodied();
BfAstNode* GetRefNode(); BfAstNode* GetRefNode();
}; };