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

Fixed const eval issues with generics

This commit is contained in:
Brian Fiete 2021-01-05 05:55:41 -08:00
parent abd883212f
commit 301f9eb1c1
3 changed files with 63 additions and 16 deletions

View file

@ -5078,11 +5078,9 @@ BfTypedValue BfExprEvaluator::CreateCall(BfAstNode* targetSrc, BfMethodInstance*
if (mModule->mCompiler->mCEMachine != NULL) if (mModule->mCompiler->mCEMachine != NULL)
{ {
if ((mModule->mIsConstModule) && (!methodInstance->mReturnType->IsVar())) bool doConstReturn = false;
{
mModule->mCompiler->mCEMachine->QueueMethod(methodInstance, func); if ((mBfEvalExprFlags & BfEvalExprFlags_ConstEval) != 0)
}
else if ((mBfEvalExprFlags & BfEvalExprFlags_ConstEval) != 0)
{ {
if (mFunctionBindResult != NULL) if (mFunctionBindResult != NULL)
{ {
@ -5092,6 +5090,10 @@ BfTypedValue BfExprEvaluator::CreateCall(BfAstNode* targetSrc, BfMethodInstance*
{ {
mModule->Fail("Const evaluation not allowed with cascade operator", targetSrc); mModule->Fail("Const evaluation not allowed with cascade operator", targetSrc);
} }
else if (methodInstance->mIsUnspecialized)
{
doConstReturn = true;
}
else else
{ {
CeEvalFlags evalFlags = CeEvalFlags_None; CeEvalFlags evalFlags = CeEvalFlags_None;
@ -5103,6 +5105,33 @@ BfTypedValue BfExprEvaluator::CreateCall(BfAstNode* targetSrc, BfMethodInstance*
} }
} }
} }
else if (mModule->mIsConstModule)
{
if (methodInstance->mIsUnspecialized)
{
doConstReturn = true;
}
else
{
mModule->mCompiler->mCEMachine->QueueMethod(methodInstance, func);
}
}
if (doConstReturn)
{
if ((returnType->IsVar()) && (mExpectingType != NULL))
returnType = mExpectingType;
if (returnType->IsRef())
{
return _GetDefaultReturnValue();
}
else
{
return mModule->GetDefaultTypedValue(returnType, true, BfDefaultValueKind_Undef);
}
return _GetDefaultReturnValue();
}
} }
if (!forceBind) if (!forceBind)

View file

@ -1655,7 +1655,6 @@ BfLocalVariable* BfModule::HandleVariableDeclaration(BfVariableDeclaration* varD
initValue = constResolver.Resolve(varDecl->mInitializer, resolvedType, BfConstResolveFlag_RemapFromStringId); initValue = constResolver.Resolve(varDecl->mInitializer, resolvedType, BfConstResolveFlag_RemapFromStringId);
if (!initValue) if (!initValue)
initValue = GetDefaultTypedValue(resolvedType); initValue = GetDefaultTypedValue(resolvedType);
} }
else if (varDecl->mInitializer->IsA<BfUninitializedExpression>()) else if (varDecl->mInitializer->IsA<BfUninitializedExpression>())
{ {

View file

@ -2715,13 +2715,27 @@ BfError* CeMachine::Fail(const CeFrame& curFrame, const StringImpl& str)
err += " "; err += " ";
} }
auto contextMethodInstance = mCurModule->mCurMethodInstance;
if (stackIdx > 1)
{
auto func = mCallStack[stackIdx - 1].mFunction;
contextMethodInstance = func->mCeFunctionInfo->mMethodInstance;
}
err += StrFormat("in const evaluation of "); err += StrFormat("in const evaluation of ");
//
{
SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(mCeModule->mCurTypeInstance, (contextMethodInstance != NULL) ? contextMethodInstance->GetOwner() : NULL);
SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(mCeModule->mCurMethodInstance, contextMethodInstance);
if (ceFunction->mMethodInstance != NULL) if (ceFunction->mMethodInstance != NULL)
err += mCeModule->MethodToString(ceFunction->mMethodInstance, BfMethodNameFlag_OmitParams); err += mCeModule->MethodToString(ceFunction->mMethodInstance, BfMethodNameFlag_OmitParams);
else else
{ {
err += mCeModule->MethodToString(ceFunction->mCeInnerFunctionInfo->mOwner->mMethodInstance, BfMethodNameFlag_OmitParams); err += mCeModule->MethodToString(ceFunction->mCeInnerFunctionInfo->mOwner->mMethodInstance, BfMethodNameFlag_OmitParams);
} }
}
if (emitEntry != NULL) if (emitEntry != NULL)
err += StrFormat(" at line% d:%d in %s", emitEntry->mLine + 1, emitEntry->mColumn + 1, ceFunction->mFiles[emitEntry->mFile].c_str()); err += StrFormat(" at line% d:%d in %s", emitEntry->mLine + 1, emitEntry->mColumn + 1, ceFunction->mFiles[emitEntry->mFile].c_str());
@ -2831,6 +2845,8 @@ addr_ce CeMachine::GetReflectType(int typeId)
if (!mReflectMap.TryAdd(typeId, NULL, &addrPtr)) if (!mReflectMap.TryAdd(typeId, NULL, &addrPtr))
return *addrPtr; return *addrPtr;
SetAndRestoreValue<bool> ignoreWrites(mCeModule->mBfIRBuilder->mIgnoreWrites, false);
if (mCeModule->mContext->mBfTypeType == NULL) if (mCeModule->mContext->mBfTypeType == NULL)
mCeModule->mContext->ReflectInit(); mCeModule->mContext->ReflectInit();
@ -2981,9 +2997,12 @@ void CeMachine::DerefMethodInfo(CeFunctionInfo* ceFunctionInfo)
return; return;
BF_ASSERT(ceFunctionInfo->mMethodInstance == NULL); BF_ASSERT(ceFunctionInfo->mMethodInstance == NULL);
if (!ceFunctionInfo->mName.IsEmpty())
{
auto itr = mNamedFunctionMap.Find(ceFunctionInfo->mName); auto itr = mNamedFunctionMap.Find(ceFunctionInfo->mName);
if (itr->mValue == ceFunctionInfo) if (itr->mValue == ceFunctionInfo)
mNamedFunctionMap.Remove(itr); mNamedFunctionMap.Remove(itr);
}
delete ceFunctionInfo; delete ceFunctionInfo;
} }