mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-08 11:38:21 +02:00
Improved undef handling of unspecialized const generic arg
This commit is contained in:
parent
a30e539d29
commit
42e837301b
6 changed files with 49 additions and 33 deletions
|
@ -31,7 +31,6 @@ bool BfConstResolver::CheckAllowValue(const BfTypedValue& typedValue, BfAstNode*
|
|||
BfConstResolver::BfConstResolver(BfModule* bfModule) : BfExprEvaluator(bfModule)
|
||||
{
|
||||
mIsInvalidConstExpr = false;
|
||||
mAllowGenericConstValue = false;
|
||||
}
|
||||
|
||||
BfTypedValue BfConstResolver::Resolve(BfExpression* expr, BfType* wantType, BfConstResolveFlags flags)
|
||||
|
@ -174,30 +173,8 @@ BfTypedValue BfConstResolver::Resolve(BfExpression* expr, BfType* wantType, BfCo
|
|||
|
||||
if (mResult.mKind == BfTypedValueKind_GenericConstValue)
|
||||
{
|
||||
if (mAllowGenericConstValue)
|
||||
if ((mBfEvalExprFlags & BfEvalExprFlags_AllowGenericConstValue) != 0)
|
||||
return mResult;
|
||||
|
||||
auto genericParamDef = mModule->GetGenericParamInstance((BfGenericParamType*)mResult.mType);
|
||||
if ((genericParamDef->mGenericParamFlags & BfGenericParamFlag_Const) != 0)
|
||||
{
|
||||
auto genericTypeConstraint = genericParamDef->mTypeConstraint;
|
||||
if (genericTypeConstraint != NULL)
|
||||
{
|
||||
auto primType = genericTypeConstraint->ToPrimitiveType();
|
||||
if (primType != NULL)
|
||||
{
|
||||
BfTypedValue result;
|
||||
result.mKind = BfTypedValueKind_Value;
|
||||
result.mType = genericTypeConstraint;
|
||||
result.mValue = mModule->mBfIRBuilder->GetUndefConstValue(mModule->mBfIRBuilder->GetPrimitiveType(primType->mTypeDef->mTypeCode));
|
||||
return result;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
BF_FATAL("Error");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (mResult)
|
||||
|
|
|
@ -24,8 +24,7 @@ enum BfConstResolveFlags
|
|||
class BfConstResolver : public BfExprEvaluator
|
||||
{
|
||||
public:
|
||||
bool mIsInvalidConstExpr;
|
||||
bool mAllowGenericConstValue;
|
||||
bool mIsInvalidConstExpr;
|
||||
|
||||
public:
|
||||
virtual bool CheckAllowValue(const BfTypedValue& typedValue, BfAstNode* refNode) override;
|
||||
|
|
|
@ -4401,6 +4401,33 @@ BfTypedValue BfExprEvaluator::LookupIdentifier(BfAstNode* refNode, const StringI
|
|||
if ((!result) && (identifierNode != NULL))
|
||||
{
|
||||
result = mModule->TryLookupGenericConstVaue(identifierNode, mExpectingType);
|
||||
if ((mBfEvalExprFlags & (BfEvalExprFlags_Comptime | BfEvalExprFlags_AllowGenericConstValue)) == BfEvalExprFlags_Comptime)
|
||||
{
|
||||
if (result.mKind == BfTypedValueKind_GenericConstValue)
|
||||
{
|
||||
auto genericParamDef = mModule->GetGenericParamInstance((BfGenericParamType*)result.mType);
|
||||
if ((genericParamDef->mGenericParamFlags & BfGenericParamFlag_Const) != 0)
|
||||
{
|
||||
auto genericTypeConstraint = genericParamDef->mTypeConstraint;
|
||||
if (genericTypeConstraint != NULL)
|
||||
{
|
||||
auto primType = genericTypeConstraint->ToPrimitiveType();
|
||||
if (primType != NULL)
|
||||
{
|
||||
BfTypedValue result;
|
||||
result.mKind = BfTypedValueKind_Value;
|
||||
result.mType = genericTypeConstraint;
|
||||
result.mValue = mModule->mBfIRBuilder->GetUndefConstValue(mModule->mBfIRBuilder->GetPrimitiveType(primType->mTypeDef->mTypeCode));
|
||||
return result;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
BF_FATAL("Error");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
mModule->FixValueActualization(result);
|
||||
}
|
||||
|
||||
|
|
|
@ -84,6 +84,7 @@ enum BfEvalExprFlags
|
|||
BfEvalExprFlags_NoCeRebuildFlags = 0x4000000,
|
||||
BfEvalExprFlags_FromConversionOp = 0x8000000,
|
||||
BfEvalExprFlags_FromConversionOp_Explicit = 0x10000000,
|
||||
BfEvalExprFlags_AllowGenericConstValue = 0x20000000,
|
||||
|
||||
BfEvalExprFlags_InheritFlags = BfEvalExprFlags_NoAutoComplete | BfEvalExprFlags_Comptime | BfEvalExprFlags_DeclType
|
||||
};
|
||||
|
|
|
@ -11020,7 +11020,7 @@ BfType* BfModule::ResolveTypeRef(BfTypeReference* typeRef, BfPopulateType popula
|
|||
BfConstResolver constResolver(this);
|
||||
BfType* intType = GetPrimitiveType(BfTypeCode_IntPtr);
|
||||
constResolver.mExpectingType = intType;
|
||||
constResolver.mAllowGenericConstValue = true;
|
||||
constResolver.mBfEvalExprFlags = (BfEvalExprFlags)(constResolver.mBfEvalExprFlags | BfEvalExprFlags_AllowGenericConstValue);
|
||||
BfTypedValue typedVal;
|
||||
{
|
||||
SetAndRestoreValue<bool> prevIgnoreErrors(mIgnoreErrors, true);
|
||||
|
@ -13029,8 +13029,20 @@ BfIRValue BfModule::CastToValue(BfAstNode* srcNode, BfTypedValue typedVal, BfTyp
|
|||
|
||||
auto undefConst = (BfConstantUndef*)constant;
|
||||
|
||||
BF_ASSERT(undefConst->mType.mKind == BfIRTypeData::TypeKind_TypeId);
|
||||
auto bfType = mContext->mTypes[undefConst->mType.mId];
|
||||
BfType* bfType = NULL;
|
||||
if (undefConst->mType.mKind == BfIRTypeData::TypeKind_TypeCode)
|
||||
{
|
||||
bfType = GetPrimitiveType((BfTypeCode)undefConst->mType.mId);
|
||||
}
|
||||
else
|
||||
{
|
||||
BF_ASSERT(undefConst->mType.mKind == BfIRTypeData::TypeKind_TypeId);
|
||||
if (undefConst->mType.mKind == BfIRTypeData::TypeKind_TypeId)
|
||||
bfType = mContext->FindTypeById(undefConst->mType.mId);
|
||||
}
|
||||
|
||||
if (bfType == NULL)
|
||||
return BfIRValue();
|
||||
|
||||
auto fakeVal = GetFakeTypedValue(bfType);
|
||||
auto val = CastToValue(srcNode, fakeVal, toType, castFlags);
|
||||
|
|
|
@ -2961,9 +2961,9 @@ BfVariant BfResolvedTypeSet::EvaluateToVariant(LookupContext* ctx, BfExpression*
|
|||
outType = NULL;
|
||||
|
||||
BfConstResolver constResolver(ctx->mModule);
|
||||
BfVariant variant;
|
||||
constResolver.mAllowGenericConstValue = true;
|
||||
BfVariant variant;
|
||||
constResolver.mBfEvalExprFlags = BfEvalExprFlags_NoCast;
|
||||
constResolver.mBfEvalExprFlags = (BfEvalExprFlags)(constResolver.mBfEvalExprFlags | BfEvalExprFlags_AllowGenericConstValue);
|
||||
constResolver.mExpectingType = ctx->mModule->GetPrimitiveType(BfTypeCode_Int64);
|
||||
auto result = constResolver.Resolve(expr);
|
||||
if (result)
|
||||
|
@ -3532,7 +3532,7 @@ int BfResolvedTypeSet::DoHash(BfTypeReference* typeRef, LookupContext* ctx, BfHa
|
|||
|
||||
BfConstResolver constResolver(ctx->mModule);
|
||||
BfType* intType = ctx->mModule->GetPrimitiveType(BfTypeCode_IntPtr);
|
||||
constResolver.mAllowGenericConstValue = true;
|
||||
constResolver.mBfEvalExprFlags = (BfEvalExprFlags)(constResolver.mBfEvalExprFlags | BfEvalExprFlags_AllowGenericConstValue);
|
||||
constResolver.mExpectingType = intType;
|
||||
BfTypedValue typedVal = constResolver.Resolve(sizeExpr, NULL, BfConstResolveFlag_ArrayInitSize);
|
||||
if (typedVal.mKind == BfTypedValueKind_GenericConstValue)
|
||||
|
@ -4792,7 +4792,7 @@ bool BfResolvedTypeSet::Equals(BfType* lhs, BfTypeReference* rhs, LookupContext*
|
|||
SetAndRestoreValue<bool> prevIgnoreError(ctx->mModule->mIgnoreErrors, true);
|
||||
BfConstResolver constResolver(ctx->mModule);
|
||||
BfType* intType = ctx->mModule->GetPrimitiveType(BfTypeCode_IntPtr);
|
||||
constResolver.mAllowGenericConstValue = true;
|
||||
constResolver.mBfEvalExprFlags = (BfEvalExprFlags)(constResolver.mBfEvalExprFlags | BfEvalExprFlags_AllowGenericConstValue);
|
||||
constResolver.mExpectingType = intType;
|
||||
BfTypedValue typedVal = constResolver.Resolve(sizeExpr, NULL, BfConstResolveFlag_ArrayInitSize);
|
||||
if (typedVal.mKind == BfTypedValueKind_GenericConstValue)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue