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

Improved generic constraint const conversion handling

This commit is contained in:
Brian Fiete 2022-05-06 12:49:57 -07:00
parent c750ed076c
commit e5c4321440
4 changed files with 35 additions and 3 deletions

View file

@ -461,6 +461,18 @@ int BfIRConstHolder::IsZero(BfIRValue value)
return -1; return -1;
} }
bool BfIRConstHolder::IsConstValue(BfIRValue value)
{
auto constant = GetConstant(value);
if (constant == NULL)
return false;
if (constant->mConstType == BfConstType_GlobalVar)
return false;
return true;
}
int BfIRConstHolder::CheckConstEquality(BfIRValue lhs, BfIRValue rhs) int BfIRConstHolder::CheckConstEquality(BfIRValue lhs, BfIRValue rhs)
{ {
auto constLHS = GetConstant(lhs); auto constLHS = GetConstant(lhs);

View file

@ -948,6 +948,7 @@ public:
BfConstant* GetConstant(BfIRValue id); BfConstant* GetConstant(BfIRValue id);
bool TryGetBool(BfIRValue id, bool& boolVal); bool TryGetBool(BfIRValue id, bool& boolVal);
int IsZero(BfIRValue val); int IsZero(BfIRValue val);
bool IsConstValue(BfIRValue val);
int CheckConstEquality(BfIRValue lhs, BfIRValue rhs); // -1 = fail, 0 = false, 1 = true int CheckConstEquality(BfIRValue lhs, BfIRValue rhs); // -1 = fail, 0 = false, 1 = true
//void WriteConstant(void* data, BeConstant* constVal); //void WriteConstant(void* data, BeConstant* constVal);
@ -970,7 +971,7 @@ public:
BfIRValue CreateGlobalVariableConstant(BfIRType varType, bool isConstant, BfIRLinkageType linkageType, BfIRValue initializer, const StringImpl& name, bool isTLS = false); BfIRValue CreateGlobalVariableConstant(BfIRType varType, bool isConstant, BfIRLinkageType linkageType, BfIRValue initializer, const StringImpl& name, bool isTLS = false);
bool WriteConstant(BfIRValue val, void* ptr, BfType* type); bool WriteConstant(BfIRValue val, void* ptr, BfType* type);
BfIRValue ReadConstant(void* ptr, BfType* type); BfIRValue ReadConstant(void* ptr, BfType* type);
}; };
enum BfIRPopulateType enum BfIRPopulateType

View file

@ -13510,7 +13510,15 @@ BfIRValue BfModule::CastToValue(BfAstNode* srcNode, BfTypedValue typedVal, BfTyp
// .. and we can convert the constraint's toType to OUR toType then we're good // .. and we can convert the constraint's toType to OUR toType then we're good
auto opToVal = genericParam->mExternType; auto opToVal = genericParam->mExternType;
if (CanCast(BfTypedValue(BfIRValue::sValueless, opToVal), toType, BfCastFlags_NoConversionOperator)) if (CanCast(BfTypedValue(BfIRValue::sValueless, opToVal), toType, BfCastFlags_NoConversionOperator))
return mBfIRBuilder->GetFakeVal(); {
if (mBfIRBuilder->IsConstValue(typedVal.mValue))
{
// Retain constness
return mBfIRBuilder->GetUndefConstValue(mBfIRBuilder->MapType(toType));
}
else
return mBfIRBuilder->GetFakeVal();
}
} }
} }
} }
@ -13535,7 +13543,15 @@ BfIRValue BfModule::CastToValue(BfAstNode* srcNode, BfTypedValue typedVal, BfTyp
// .. and we can convert the constraint's toType to OUR toType then we're good // .. and we can convert the constraint's toType to OUR toType then we're good
auto opToVal = genericParam->mExternType; auto opToVal = genericParam->mExternType;
if (CanCast(BfTypedValue(BfIRValue::sValueless, opToVal), toType, BfCastFlags_NoConversionOperator)) if (CanCast(BfTypedValue(BfIRValue::sValueless, opToVal), toType, BfCastFlags_NoConversionOperator))
return mBfIRBuilder->GetFakeVal(); {
if (mBfIRBuilder->IsConstValue(typedVal.mValue))
{
// Retain constness
return mBfIRBuilder->GetUndefConstValue(mBfIRBuilder->MapType(toType));
}
else
return mBfIRBuilder->GetFakeVal();
}
} }
} }
} }

View file

@ -4409,6 +4409,9 @@ BfIRValue CeContext::CreateConstant(BfModule* module, uint8* ptr, BfType* bfType
if (bfType->IsTypedPrimitive()) if (bfType->IsTypedPrimitive())
return CreateConstant(module, ptr, bfType->GetUnderlyingType(), outType); return CreateConstant(module, ptr, bfType->GetUnderlyingType(), outType);
if (bfType->IsGenericParam())
return irBuilder->GetUndefConstValue(irBuilder->MapType(bfType));
if (bfType->IsTypeInstance()) if (bfType->IsTypeInstance())
{ {
auto typeInst = bfType->ToTypeInstance(); auto typeInst = bfType->ToTypeInstance();