mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-10 20:42:21 +02:00
Improvements to auto-impl properties
This commit is contained in:
parent
36e0a3a375
commit
06f4eb9576
8 changed files with 113 additions and 61 deletions
|
@ -569,6 +569,7 @@ enum BfTypedValueKind
|
|||
{
|
||||
BfTypedValueKind_Addr,
|
||||
BfTypedValueKind_CopyOnMutateAddr,
|
||||
BfTypedValueKind_CopyOnMutateAddr_Derived,
|
||||
BfTypedValueKind_ReadOnlyAddr,
|
||||
BfTypedValueKind_TempAddr,
|
||||
BfTypedValueKind_RestrictedTempAddr,
|
||||
|
@ -688,7 +689,7 @@ public:
|
|||
|
||||
bool IsCopyOnMutate() const
|
||||
{
|
||||
return (mKind == BfTypedValueKind_CopyOnMutateAddr);
|
||||
return (mKind == BfTypedValueKind_CopyOnMutateAddr) || (mKind == BfTypedValueKind_CopyOnMutateAddr_Derived);
|
||||
}
|
||||
|
||||
bool IsReadOnly() const
|
||||
|
|
|
@ -4591,22 +4591,29 @@ BfTypedValue BfExprEvaluator::LoadProperty(BfAstNode* targetSrc, BfTypedValue ta
|
|||
{
|
||||
bool needsCopy = true;
|
||||
|
||||
if (setter == NULL)
|
||||
if (BfNodeIsA<BfRefTypeRef>(prop->mTypeRef))
|
||||
{
|
||||
if (((mModule->mCurMethodInstance->mMethodDef->mMethodType == BfMethodType_Ctor)) &&
|
||||
(target.mType == mModule->mCurTypeInstance))
|
||||
{
|
||||
// Allow writing inside ctor
|
||||
}
|
||||
else
|
||||
{
|
||||
result.MakeReadOnly();
|
||||
needsCopy = false;
|
||||
}
|
||||
// Allow full ref
|
||||
}
|
||||
else
|
||||
{
|
||||
if (setter == NULL)
|
||||
{
|
||||
if (((mModule->mCurMethodInstance->mMethodDef->mMethodType == BfMethodType_Ctor)) &&
|
||||
(target.mType == mModule->mCurTypeInstance))
|
||||
{
|
||||
// Allow writing inside ctor
|
||||
}
|
||||
else
|
||||
{
|
||||
result.MakeReadOnly();
|
||||
needsCopy = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (result.mKind == BfTypedValueKind_Addr)
|
||||
result.mKind = BfTypedValueKind_CopyOnMutateAddr;
|
||||
if (result.mKind == BfTypedValueKind_Addr)
|
||||
result.mKind = BfTypedValueKind_CopyOnMutateAddr;
|
||||
}
|
||||
|
||||
mPropDef = NULL;
|
||||
mPropSrc = NULL;
|
||||
|
@ -4890,10 +4897,7 @@ BfTypedValue BfExprEvaluator::LoadField(BfAstNode* targetSrc, BfTypedValue targe
|
|||
mModule->mBfIRBuilder->CreateStore(target.mValue, elementAddr);
|
||||
target = BfTypedValue(allocaInst, primStructType, true);
|
||||
}
|
||||
|
||||
if (target.IsCopyOnMutate())
|
||||
target = mModule->CopyValue(target);
|
||||
|
||||
|
||||
BfTypedValue targetValue;
|
||||
if ((target.mType != typeInstance) && (!target.IsSplat()))
|
||||
{
|
||||
|
@ -4972,6 +4976,9 @@ BfTypedValue BfExprEvaluator::LoadField(BfAstNode* targetSrc, BfTypedValue targe
|
|||
{
|
||||
if ((wantsReadOnly) && (retVal.IsAddr()) && (!retVal.IsReadOnly()))
|
||||
retVal.mKind = BfTypedValueKind_ReadOnlyAddr;
|
||||
else if ((target.IsCopyOnMutate()) && (retVal.IsAddr()))
|
||||
retVal.mKind = BfTypedValueKind_CopyOnMutateAddr_Derived;
|
||||
|
||||
mIsHeapReference = true;
|
||||
}
|
||||
|
||||
|
@ -6803,6 +6810,9 @@ void BfExprEvaluator::PushThis(BfAstNode* targetSrc, BfTypedValue argVal, BfMeth
|
|||
|
||||
if (((argVal.mType->IsComposite()) || (argVal.mType->IsTypedPrimitive())))
|
||||
{
|
||||
if (argVal.IsCopyOnMutate())
|
||||
argVal = mModule->CopyValue(argVal);
|
||||
|
||||
if ((argVal.IsReadOnly()) || (!argVal.IsAddr()))
|
||||
{
|
||||
if (!skipMutCheck)
|
||||
|
@ -18297,11 +18307,8 @@ bool BfExprEvaluator::CheckIsBase(BfAstNode* checkNode)
|
|||
return true;
|
||||
}
|
||||
|
||||
bool BfExprEvaluator::CheckModifyResult(BfTypedValue typedVal, BfAstNode* refNode, const char* modifyType, bool onlyNeedsMut, bool emitWarning, bool skipCopyOnMutate)
|
||||
{
|
||||
if ((!skipCopyOnMutate) && (typedVal.IsCopyOnMutate()))
|
||||
typedVal = mModule->CopyValue(typedVal);
|
||||
|
||||
bool BfExprEvaluator::CheckModifyResult(BfTypedValue& typedVal, BfAstNode* refNode, const char* modifyType, bool onlyNeedsMut, bool emitWarning, bool skipCopyOnMutate)
|
||||
{
|
||||
BfLocalVariable* localVar = NULL;
|
||||
bool isCapturedLocal = false;
|
||||
if (mResultLocalVar != NULL)
|
||||
|
@ -18336,6 +18343,9 @@ bool BfExprEvaluator::CheckModifyResult(BfTypedValue typedVal, BfAstNode* refNod
|
|||
}
|
||||
|
||||
bool canModify = typedVal.CanModify();
|
||||
if (((typedVal.mKind == BfTypedValueKind_TempAddr) || (typedVal.mKind == BfTypedValueKind_CopyOnMutateAddr_Derived)) &&
|
||||
(strcmp(modifyType, "assign to") == 0))
|
||||
mModule->Warn(0, "Assigning to temporary copy of a value. Consider using 'ref' in value source declaration.", refNode);
|
||||
|
||||
auto _Fail = [&](const StringImpl& error, BfAstNode* refNode)
|
||||
{
|
||||
|
@ -18497,6 +18507,9 @@ bool BfExprEvaluator::CheckModifyResult(BfTypedValue typedVal, BfAstNode* refNod
|
|||
return false;
|
||||
}
|
||||
|
||||
if ((!skipCopyOnMutate) && (typedVal.IsCopyOnMutate()))
|
||||
typedVal = mModule->CopyValue(typedVal);
|
||||
|
||||
return mModule->CheckModifyValue(typedVal, refNode, modifyType);
|
||||
}
|
||||
|
||||
|
@ -19250,6 +19263,11 @@ void BfExprEvaluator::PerformAssignment(BfAssignmentExpression* assignExpr, bool
|
|||
return;
|
||||
}
|
||||
|
||||
if (ptr.mKind == BfTypedValueKind_CopyOnMutateAddr)
|
||||
ptr.mKind = BfTypedValueKind_Addr;
|
||||
else if (ptr.IsCopyOnMutate())
|
||||
ptr = mModule->CopyValue(ptr);
|
||||
|
||||
BF_ASSERT(convVal);
|
||||
if ((convVal) && (convVal.mType->IsNull()) && (ptr.mType->IsNullable()))
|
||||
{
|
||||
|
|
|
@ -449,7 +449,7 @@ public:
|
|||
void MarkResultAssigned();
|
||||
void MakeResultAsValue();
|
||||
bool CheckIsBase(BfAstNode* checkNode);
|
||||
bool CheckModifyResult(BfTypedValue typeValue, BfAstNode* refNode, const char* modifyType, bool onlyNeedsMut = false, bool emitWarning = false, bool skipCopyOnMutate = false);
|
||||
bool CheckModifyResult(BfTypedValue& typeValue, BfAstNode* refNode, const char* modifyType, bool onlyNeedsMut = false, bool emitWarning = false, bool skipCopyOnMutate = false);
|
||||
bool CheckGenericCtor(BfGenericParamType* genericParamType, BfResolvedArgs& argValues, BfAstNode* targetSrc);
|
||||
BfTypedValue LoadProperty(BfAstNode* targetSrc, BfTypedValue target, BfTypeInstance* typeInstance, BfPropertyDef* prop, BfLookupFieldFlags flags, BfCheckedKind checkedKind, bool isInline);
|
||||
BfTypedValue LoadField(BfAstNode* targetSrc, BfTypedValue target, BfTypeInstance* typeInstance, BfFieldDef* fieldDef, BfLookupFieldFlags flags);
|
||||
|
|
|
@ -20710,6 +20710,9 @@ void BfModule::ProcessMethod(BfMethodInstance* methodInstance, bool isInlineDup,
|
|||
}
|
||||
else if (methodDef->mMethodType == BfMethodType_PropertyGetter)
|
||||
{
|
||||
if ((methodInstance->mReturnType->IsRef()) && (!methodDef->mIsMutating))
|
||||
Fail("Auto-implemented ref property getters must declare 'mut'", methodInstance->mMethodDef->GetRefNode());
|
||||
|
||||
if (methodInstance->mReturnType->IsValuelessType())
|
||||
{
|
||||
mBfIRBuilder->CreateRetVoid();
|
||||
|
@ -20736,8 +20739,14 @@ void BfModule::ProcessMethod(BfMethodInstance* methodInstance, bool isInlineDup,
|
|||
lookupValue = BfTypedValue(mBfIRBuilder->CreateInBoundsGEP(GetThis().mValue, 0, fieldInstance->mDataIdx), fieldInstance->mResolvedType, true);
|
||||
else
|
||||
lookupValue = ExtractValue(GetThis(), fieldInstance, fieldInstance->mDataIdx);
|
||||
if (!methodInstance->mReturnType->IsRef())
|
||||
if (methodInstance->mReturnType->IsRef())
|
||||
{
|
||||
if ((!lookupValue.IsAddr()) && (!lookupValue.mType->IsValuelessType()))
|
||||
lookupValue = MakeAddressable(lookupValue);
|
||||
}
|
||||
else
|
||||
lookupValue = LoadOrAggregateValue(lookupValue);
|
||||
|
||||
CreateReturn(lookupValue.mValue);
|
||||
EmitLifetimeEnds(&mCurMethodState->mHeadScope);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue