1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 19:48:20 +02:00

Fixed default parameters requiring conversion operators

This commit is contained in:
Brian Fiete 2020-03-11 07:57:20 -07:00
parent 5a63fec168
commit 7458a90b5b
7 changed files with 98 additions and 61 deletions

View file

@ -91,7 +91,7 @@ BfTypedValue BfConstResolver::Resolve(BfExpression* expr, BfType* wantType, BfCo
return BfTypedValue(mModule->GetStringObjectValue(stringId), mResult.mType);
}
return BfTypedValue(mModule->GetConstValue32(stringId), toType);
return BfTypedValue(mModule->mBfIRBuilder->CreateConst(BfTypeCode_StringId, stringId), toType);
}
}
}

View file

@ -5389,6 +5389,8 @@ BfTypedValue BfExprEvaluator::CreateCall(BfAstNode* targetSrc, const BfTypedValu
if (!argValue)
{
argValue = mModule->GetTypedValueFromConstant(foreignConst, methodInstance->GetOwner()->mConstHolder, wantType);
if (!argValue)
mModule->Fail("Default parameter value failed", targetSrc);
}
}
else

View file

@ -573,12 +573,12 @@ BfIRValue BfIRConstHolder::CreateConst(BfConstant* fromConst, BfIRConstHolder* f
}
return CreateConstArray(constArray->mType, copiedVals);
}
else if ((IsInt(fromConst->mTypeCode)) || (fromConst->mTypeCode == BfTypeCode_Boolean))
else if ((IsInt(fromConst->mTypeCode)) || (fromConst->mTypeCode == BfTypeCode_Boolean) || (fromConst->mTypeCode == BfTypeCode_StringId))
{
return CreateConst(fromConst->mTypeCode, fromConst->mUInt64);
}
else if ((fromConst->mTypeCode == BfTypeCode_Single) || (fromConst->mTypeCode == BfTypeCode_Double))
{
{
return CreateConst(fromConst->mTypeCode, fromConst->mDouble);
}
else if (fromConst->mTypeCode == BfTypeCode_NullPtr)

View file

@ -81,8 +81,9 @@ enum BfTypeCode : uint8
{
BfTypeCode_None,
BfTypeCode_CharPtr,
BfTypeCode_StringId,
BfTypeCode_Pointer,
BfTypeCode_NullPtr,
BfTypeCode_NullPtr,
BfTypeCode_Self,
BfTypeCode_Dot,
BfTypeCode_Var,

View file

@ -1518,7 +1518,7 @@ int BfModule::GetStringPoolIdx(BfIRValue constantStr, BfIRConstHolder* constHold
constHolder = mBfIRBuilder;
auto constant = constHolder->GetConstant(constantStr);
if (constant->mTypeCode == BfTypeCode_Int32)
if (constant->mTypeCode == BfTypeCode_StringId)
{
return constant->mInt32;
}
@ -6075,7 +6075,7 @@ BfIRFunction BfModule::GetIntrinsic(BfMethodInstance* methodInstance, bool repor
auto constant = methodOwner->mConstHolder->GetConstant(customAttribute.mCtorArgs[0]);
String error;
if ((constant != NULL) && (BfIRConstHolder::IsInt(constant->mTypeCode)))
if ((constant != NULL) && (constant->mTypeCode = BfTypeCode_StringId))
{
int stringId = constant->mInt32;
auto entry = mContext->mStringObjectIdMap[stringId];
@ -9335,7 +9335,7 @@ void BfModule::CurrentAddToConstHolder(BfIRValue& irVal)
int stringPoolIdx = GetStringPoolIdx(irVal, mBfIRBuilder);
if (stringPoolIdx != -1)
{
irVal = mCurTypeInstance->GetOrCreateConstHolder()->CreateConst(BfTypeCode_Int32, stringPoolIdx);
irVal = mCurTypeInstance->GetOrCreateConstHolder()->CreateConst(BfTypeCode_StringId, stringPoolIdx);
return;
}
@ -9374,56 +9374,70 @@ void BfModule::ClearConstData()
}
BfTypedValue BfModule::GetTypedValueFromConstant(BfConstant* constant, BfIRConstHolder* constHolder, BfType* wantType)
{
bool isString = false;
isString = (wantType->IsObject()) && (wantType->ToTypeInstance()->mTypeDef == mCompiler->mStringTypeDef);
if (wantType->IsPointer())
isString = true;
if (!isString)
{
switch (constant->mTypeCode)
{
switch (constant->mTypeCode)
case BfTypeCode_StringId:
case BfTypeCode_Boolean:
case BfTypeCode_Int8:
case BfTypeCode_UInt8:
case BfTypeCode_Int16:
case BfTypeCode_UInt16:
case BfTypeCode_Int32:
case BfTypeCode_UInt32:
case BfTypeCode_Int64:
case BfTypeCode_UInt64:
case BfTypeCode_IntPtr:
case BfTypeCode_UIntPtr:
case BfTypeCode_IntUnknown:
case BfTypeCode_UIntUnknown:
case BfTypeCode_Char8:
case BfTypeCode_Char16:
case BfTypeCode_Char32:
case BfTypeCode_Single:
case BfTypeCode_Double:
{
case BfTypeCode_Boolean:
case BfTypeCode_Int8:
case BfTypeCode_UInt8:
case BfTypeCode_Int16:
case BfTypeCode_UInt16:
case BfTypeCode_Int32:
case BfTypeCode_UInt32:
case BfTypeCode_Int64:
case BfTypeCode_UInt64:
case BfTypeCode_IntPtr:
case BfTypeCode_UIntPtr:
case BfTypeCode_IntUnknown:
case BfTypeCode_UIntUnknown:
case BfTypeCode_Char8:
case BfTypeCode_Char16:
case BfTypeCode_Char32:
case BfTypeCode_Single:
case BfTypeCode_Double:
auto constVal = mBfIRBuilder->CreateConst(constant, constHolder);
BfTypedValue typedValue;
if (constant->mTypeCode == BfTypeCode_StringId)
{
if ((wantType->IsInstanceOf(mCompiler->mStringTypeDef)) ||
((wantType->IsPointer()) && (wantType->GetUnderlyingType() == GetPrimitiveType(BfTypeCode_Char8))))
{
typedValue = BfTypedValue(ConstantToCurrent(constant, constHolder, wantType), wantType);
return typedValue;
}
auto stringType = ResolveTypeDef(mCompiler->mStringTypeDef);
typedValue = BfTypedValue(ConstantToCurrent(constant, constHolder, stringType), stringType);
}
if (!typedValue)
{
auto constVal = mBfIRBuilder->CreateConst(constant, constHolder);
auto typedValue = BfTypedValue(constVal, GetPrimitiveType(constant->mTypeCode));
if (typedValue.mType == wantType)
return typedValue;
if (wantType->IsTypedPrimitive())
{
if (typedValue.mType == wantType->GetUnderlyingType())
{
typedValue.mType = wantType;
return typedValue;
}
}
auto castedTypedValue = Cast(NULL, typedValue, wantType, (BfCastFlags)(BfCastFlags_SilentFail | BfCastFlags_Explicit));
BF_ASSERT(castedTypedValue);
return castedTypedValue;
typedValue = BfTypedValue(constVal, GetPrimitiveType(constant->mTypeCode));
}
break;
default: break;
if (typedValue.mType == wantType)
return typedValue;
if (wantType->IsTypedPrimitive())
{
if (typedValue.mType == wantType->GetUnderlyingType())
{
typedValue.mType = wantType;
return typedValue;
}
}
auto castedTypedValue = Cast(NULL, typedValue, wantType, (BfCastFlags)(BfCastFlags_SilentFail | BfCastFlags_Explicit));
if (!castedTypedValue)
return BfTypedValue();
return castedTypedValue;
}
break;
default: break;
}
BfIRValue irValue = ConstantToCurrent(constant, constHolder, wantType);
BF_ASSERT(irValue);
if (!irValue)
@ -9438,17 +9452,19 @@ BfIRValue BfModule::ConstantToCurrent(BfConstant* constant, BfIRConstHolder* con
return GetDefaultValue(wantType);
}
bool isString = false;
isString = (wantType->IsObject()) && (wantType->ToTypeInstance()->mTypeDef == mCompiler->mStringTypeDef);
if (((wantType->IsPointer()) || (isString)) && (mBfIRBuilder->IsInt(constant->mTypeCode)))
{
const StringImpl& str = mContext->mStringObjectIdMap[constant->mInt32].mString;
BfIRValue stringObjConst = GetStringObjectValue(str);
if (wantType->IsObject())
return stringObjConst;
return GetStringCharPtr(stringObjConst);
}
if (constant->mTypeCode == BfTypeCode_StringId)
{
if ((wantType->IsInstanceOf(mCompiler->mStringTypeDef)) ||
((wantType->IsPointer()) && (wantType->GetUnderlyingType() == GetPrimitiveType(BfTypeCode_Char8))))
{
const StringImpl& str = mContext->mStringObjectIdMap[constant->mInt32].mString;
BfIRValue stringObjConst = GetStringObjectValue(str);
if (wantType->IsPointer())
return GetStringCharPtr(stringObjConst);
return stringObjConst;
}
}
if (constant->mConstType == BfConstType_Array)
{
auto elementType = wantType->GetUnderlyingType();

View file

@ -4536,6 +4536,9 @@ BfPrimitiveType* BfModule::GetPrimitiveType(BfTypeCode typeCode)
case BfTypeCode_UIntUnknown:
primType = (BfPrimitiveType*)ResolveTypeDef(mSystem->mTypeUIntUnknown);
break;
case BfTypeCode_StringId:
BF_FATAL("Invalid use of StringId");
break;
default: break;
}
mContext->mPrimitiveTypes[typeCode] = primType;

View file

@ -231,6 +231,13 @@ namespace Tests
{
return val.mA;
}
public static implicit operator Self(int val)
{
IntStruct sVal;
sVal.mA = val;
return sVal;
}
}
[Test]
@ -244,6 +251,12 @@ namespace Tests
const String cStrD = "D";
const char8* cStrPD = "D";
public static void TestDefaults(StringView sv = "ABC", IntStruct intStruct = 123)
{
Test.Assert(sv == "ABC");
Test.Assert(intStruct.mA == 123);
}
[Test]
public static void TestStringOp()
{
@ -254,6 +267,8 @@ namespace Tests
const char8* cStr3 = "A" + "B";
const char8* cStr4 = cStr1 + "C" + cStrPD;
Test.Assert(StringView(cStr4) == "ABCD");
TestDefaults();
}
}
}