1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-09 12:02:21 +02:00

Properly throw error on capture specifier in non-lambda allocation

This commit is contained in:
Brian Fiete 2022-01-29 14:29:25 -05:00
parent c23d44502b
commit 8cccec20fa
4 changed files with 25 additions and 20 deletions

View file

@ -3760,7 +3760,8 @@ void BfExprEvaluator::Visit(BfStringInterpolationExpression* stringInterpolation
auto stringType = mModule->ResolveTypeDef(mModule->mCompiler->mStringTypeDef)->ToTypeInstance(); auto stringType = mModule->ResolveTypeDef(mModule->mCompiler->mStringTypeDef)->ToTypeInstance();
BfTokenNode* newToken = NULL; BfTokenNode* newToken = NULL;
BfAllocTarget allocTarget = ResolveAllocTarget(stringInterpolationExpression->mAllocNode, newToken); BfAllocTarget allocTarget;
ResolveAllocTarget(allocTarget, stringInterpolationExpression->mAllocNode, newToken);
CreateObject(NULL, stringInterpolationExpression->mAllocNode, stringType); CreateObject(NULL, stringInterpolationExpression->mAllocNode, stringType);
BfTypedValue newString = mResult; BfTypedValue newString = mResult;
@ -11911,7 +11912,8 @@ void BfExprEvaluator::Visit(BfDelegateBindExpression* delegateBindExpr)
} }
BfTokenNode* newToken = NULL; BfTokenNode* newToken = NULL;
BfAllocTarget allocTarget = ResolveAllocTarget(delegateBindExpr->mNewToken, newToken); BfAllocTarget allocTarget;
ResolveAllocTarget(allocTarget, delegateBindExpr->mNewToken, newToken);
SizedArray<BfTypedValueExpression, 4> typedValueExprs; SizedArray<BfTypedValueExpression, 4> typedValueExprs;
SizedArray<BfExpression*, 4> args; SizedArray<BfExpression*, 4> args;
@ -12987,7 +12989,7 @@ BfLambdaInstance* BfExprEvaluator::GetLambdaInstance(BfLambdaBindExpression* lam
auto varMethodState = methodState.mPrevMethodState; auto varMethodState = methodState.mPrevMethodState;
bool hasExplicitCaptureNames = false; bool hasExplicitCaptureNames = false;
for (auto& captureEntry : allocTarget.mCaptureInfo.mCaptures) for (auto& captureEntry : allocTarget.mCaptureInfo->mCaptures)
{ {
if (captureEntry.mNameNode == NULL) if (captureEntry.mNameNode == NULL)
{ {
@ -13029,7 +13031,7 @@ BfLambdaInstance* BfExprEvaluator::GetLambdaInstance(BfLambdaBindExpression* lam
auto varMethodState = methodState.mPrevMethodState; auto varMethodState = methodState.mPrevMethodState;
while (varMethodState != NULL) while (varMethodState != NULL)
{ {
for (auto& captureEntry : allocTarget.mCaptureInfo.mCaptures) for (auto& captureEntry : allocTarget.mCaptureInfo->mCaptures)
{ {
if (captureEntry.mNameNode != NULL) if (captureEntry.mNameNode != NULL)
{ {
@ -13134,10 +13136,10 @@ BfLambdaInstance* BfExprEvaluator::GetLambdaInstance(BfLambdaBindExpression* lam
auto _GetCaptureType = [&](const StringImpl& str) auto _GetCaptureType = [&](const StringImpl& str)
{ {
if (allocTarget.mCaptureInfo.mCaptures.IsEmpty()) if (allocTarget.mCaptureInfo->mCaptures.IsEmpty())
return BfCaptureType_Copy; return BfCaptureType_Copy;
for (auto& captureEntry : allocTarget.mCaptureInfo.mCaptures) for (auto& captureEntry : allocTarget.mCaptureInfo->mCaptures)
{ {
if ((captureEntry.mNameNode == NULL) || (captureEntry.mNameNode->Equals(str))) if ((captureEntry.mNameNode == NULL) || (captureEntry.mNameNode->Equals(str)))
{ {
@ -13274,7 +13276,7 @@ BfLambdaInstance* BfExprEvaluator::GetLambdaInstance(BfLambdaBindExpression* lam
} }
} }
for (auto& captureEntry : allocTarget.mCaptureInfo.mCaptures) for (auto& captureEntry : allocTarget.mCaptureInfo->mCaptures)
{ {
if ((!captureEntry.mUsed) && (captureEntry.mNameNode != NULL)) if ((!captureEntry.mUsed) && (captureEntry.mNameNode != NULL))
mModule->Warn(0, "Capture specifier not used", captureEntry.mNameNode); mModule->Warn(0, "Capture specifier not used", captureEntry.mNameNode);
@ -13658,7 +13660,10 @@ BfLambdaInstance* BfExprEvaluator::GetLambdaInstance(BfLambdaBindExpression* lam
void BfExprEvaluator::Visit(BfLambdaBindExpression* lambdaBindExpr) void BfExprEvaluator::Visit(BfLambdaBindExpression* lambdaBindExpr)
{ {
BfTokenNode* newToken = NULL; BfTokenNode* newToken = NULL;
BfAllocTarget allocTarget = ResolveAllocTarget(lambdaBindExpr->mNewToken, newToken); BfCaptureInfo captureInfo;
BfAllocTarget allocTarget;
allocTarget.mCaptureInfo = &captureInfo;
ResolveAllocTarget(allocTarget, lambdaBindExpr->mNewToken, newToken);
if (mModule->mCurMethodInstance == NULL) if (mModule->mCurMethodInstance == NULL)
mModule->Fail("Invalid use of lambda bind expression", lambdaBindExpr); mModule->Fail("Invalid use of lambda bind expression", lambdaBindExpr);
@ -13957,7 +13962,8 @@ void BfExprEvaluator::CreateObject(BfObjectCreateExpression* objCreateExpr, BfAs
attributeState.mTarget = BfAttributeTargets_Alloc; attributeState.mTarget = BfAttributeTargets_Alloc;
SetAndRestoreValue<BfAttributeState*> prevAttributeState(mModule->mAttributeState, &attributeState); SetAndRestoreValue<BfAttributeState*> prevAttributeState(mModule->mAttributeState, &attributeState);
BfTokenNode* newToken = NULL; BfTokenNode* newToken = NULL;
BfAllocTarget allocTarget = ResolveAllocTarget(allocNode, newToken, &attributeState.mCustomAttributes); BfAllocTarget allocTarget;
ResolveAllocTarget(allocTarget, allocNode, newToken, &attributeState.mCustomAttributes);
bool isScopeAlloc = newToken->GetToken() == BfToken_Scope; bool isScopeAlloc = newToken->GetToken() == BfToken_Scope;
bool isAppendAlloc = newToken->GetToken() == BfToken_Append; bool isAppendAlloc = newToken->GetToken() == BfToken_Append;
@ -14982,7 +14988,8 @@ void BfExprEvaluator::Visit(BfBoxExpression* boxExpr)
}*/ }*/
BfTokenNode* newToken = NULL; BfTokenNode* newToken = NULL;
BfAllocTarget allocTarget = ResolveAllocTarget(boxExpr->mAllocNode, newToken); BfAllocTarget allocTarget;
ResolveAllocTarget(allocTarget, boxExpr->mAllocNode, newToken);
if ((boxExpr->mAllocNode != NULL) && (boxExpr->mAllocNode->mToken == BfToken_Scope)) if ((boxExpr->mAllocNode != NULL) && (boxExpr->mAllocNode->mToken == BfToken_Scope))
{ {
@ -15048,12 +15055,11 @@ void BfExprEvaluator::Visit(BfBoxExpression* boxExpr)
} }
} }
BfAllocTarget BfExprEvaluator::ResolveAllocTarget(BfAstNode* allocNode, BfTokenNode*& newToken, BfCustomAttributes** outCustomAttributes) void BfExprEvaluator::ResolveAllocTarget(BfAllocTarget& allocTarget, BfAstNode* allocNode, BfTokenNode*& newToken, BfCustomAttributes** outCustomAttributes)
{ {
auto autoComplete = GetAutoComplete(); auto autoComplete = GetAutoComplete();
BfAttributeDirective* attributeDirective = NULL; BfAttributeDirective* attributeDirective = NULL;
BfAllocTarget allocTarget;
allocTarget.mRefNode = allocNode; allocTarget.mRefNode = allocNode;
newToken = BfNodeDynCast<BfTokenNode>(allocNode); newToken = BfNodeDynCast<BfTokenNode>(allocNode);
if (newToken == NULL) if (newToken == NULL)
@ -15099,7 +15105,7 @@ BfAllocTarget BfExprEvaluator::ResolveAllocTarget(BfAstNode* allocNode, BfTokenN
if (attributeDirective != NULL) if (attributeDirective != NULL)
{ {
auto customAttrs = mModule->GetCustomAttributes(attributeDirective, BfAttributeTargets_Alloc, BfGetCustomAttributesFlags_AllowNonConstArgs, &allocTarget.mCaptureInfo); auto customAttrs = mModule->GetCustomAttributes(attributeDirective, BfAttributeTargets_Alloc, BfGetCustomAttributesFlags_AllowNonConstArgs, allocTarget.mCaptureInfo);
if (customAttrs != NULL) if (customAttrs != NULL)
{ {
for (auto& attrib : customAttrs->mAttributes) for (auto& attrib : customAttrs->mAttributes)
@ -15132,8 +15138,6 @@ BfAllocTarget BfExprEvaluator::ResolveAllocTarget(BfAstNode* allocNode, BfTokenN
delete customAttrs; delete customAttrs;
} }
} }
return allocTarget;
} }
BfTypedValue BfExprEvaluator::MakeCallableTarget(BfAstNode* targetSrc, BfTypedValue target) BfTypedValue BfExprEvaluator::MakeCallableTarget(BfAstNode* targetSrc, BfTypedValue target)

View file

@ -418,7 +418,7 @@ public:
BfType* ResolveTypeRef(BfTypeReference* typeRef, BfPopulateType populateType = BfPopulateType_Data, BfResolveTypeRefFlags resolveFlags = (BfResolveTypeRefFlags)0); BfType* ResolveTypeRef(BfTypeReference* typeRef, BfPopulateType populateType = BfPopulateType_Data, BfResolveTypeRefFlags resolveFlags = (BfResolveTypeRefFlags)0);
void ResolveGenericType(); void ResolveGenericType();
void ResolveArgValues(BfResolvedArgs& resolvedArgs, BfResolveArgsFlags flags = BfResolveArgsFlag_None); void ResolveArgValues(BfResolvedArgs& resolvedArgs, BfResolveArgsFlags flags = BfResolveArgsFlag_None);
BfAllocTarget ResolveAllocTarget(BfAstNode* newNode, BfTokenNode*& newToken, BfCustomAttributes** outCustomAttributes = NULL); void ResolveAllocTarget(BfAllocTarget& allocTarget, BfAstNode* newNode, BfTokenNode*& newToken, BfCustomAttributes** outCustomAttributes = NULL);
BfTypedValue ResolveArgValue(BfResolvedArg& resolvedArg, BfType* wantType, BfTypedValue* receivingValue = NULL, BfParamKind paramKind = BfParamKind_Normal, BfIdentifierNode* paramNameNode = NULL); BfTypedValue ResolveArgValue(BfResolvedArg& resolvedArg, BfType* wantType, BfTypedValue* receivingValue = NULL, BfParamKind paramKind = BfParamKind_Normal, BfIdentifierNode* paramNameNode = NULL);
BfMethodDef* GetPropertyMethodDef(BfPropertyDef* propDef, BfMethodType methodType, BfCheckedKind checkedKind, BfTypedValue propTarget); BfMethodDef* GetPropertyMethodDef(BfPropertyDef* propDef, BfMethodType methodType, BfCheckedKind checkedKind, BfTypedValue propTarget);
BfModuleMethodInstance GetPropertyMethodInstance(BfMethodDef* methodDef); BfModuleMethodInstance GetPropertyMethodInstance(BfMethodDef* methodDef);

View file

@ -11362,7 +11362,7 @@ void BfModule::GetCustomAttributes(BfCustomAttributes* customAttributes, BfAttri
{ {
if (captureInfo == NULL) if (captureInfo == NULL)
{ {
Fail("Capture specifiers can only be used in lambda allocations", attributesDirective); Fail("Capture specifiers can only be used in lambda allocations", tokenNode);
continue; continue;
} }

View file

@ -600,7 +600,7 @@ public:
BfTypedValue mCustomAllocator; BfTypedValue mCustomAllocator;
BfScopedInvocationTarget* mScopedInvocationTarget; BfScopedInvocationTarget* mScopedInvocationTarget;
int mAlignOverride; int mAlignOverride;
BfCaptureInfo mCaptureInfo; BfCaptureInfo* mCaptureInfo;
bool mIsFriend; bool mIsFriend;
public: public:
@ -612,6 +612,7 @@ public:
mScopedInvocationTarget = NULL; mScopedInvocationTarget = NULL;
mAlignOverride = -1; mAlignOverride = -1;
mIsFriend = false; mIsFriend = false;
mCaptureInfo = NULL;
} }
BfAllocTarget(BfScopeData* scopeData) BfAllocTarget(BfScopeData* scopeData)