mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-09 03:52:19 +02:00
Fixed default parameters requiring conversion operators
This commit is contained in:
parent
5a63fec168
commit
7458a90b5b
7 changed files with 98 additions and 61 deletions
|
@ -91,7 +91,7 @@ BfTypedValue BfConstResolver::Resolve(BfExpression* expr, BfType* wantType, BfCo
|
||||||
return BfTypedValue(mModule->GetStringObjectValue(stringId), mResult.mType);
|
return BfTypedValue(mModule->GetStringObjectValue(stringId), mResult.mType);
|
||||||
}
|
}
|
||||||
|
|
||||||
return BfTypedValue(mModule->GetConstValue32(stringId), toType);
|
return BfTypedValue(mModule->mBfIRBuilder->CreateConst(BfTypeCode_StringId, stringId), toType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5389,6 +5389,8 @@ BfTypedValue BfExprEvaluator::CreateCall(BfAstNode* targetSrc, const BfTypedValu
|
||||||
if (!argValue)
|
if (!argValue)
|
||||||
{
|
{
|
||||||
argValue = mModule->GetTypedValueFromConstant(foreignConst, methodInstance->GetOwner()->mConstHolder, wantType);
|
argValue = mModule->GetTypedValueFromConstant(foreignConst, methodInstance->GetOwner()->mConstHolder, wantType);
|
||||||
|
if (!argValue)
|
||||||
|
mModule->Fail("Default parameter value failed", targetSrc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
@ -573,12 +573,12 @@ BfIRValue BfIRConstHolder::CreateConst(BfConstant* fromConst, BfIRConstHolder* f
|
||||||
}
|
}
|
||||||
return CreateConstArray(constArray->mType, copiedVals);
|
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);
|
return CreateConst(fromConst->mTypeCode, fromConst->mUInt64);
|
||||||
}
|
}
|
||||||
else if ((fromConst->mTypeCode == BfTypeCode_Single) || (fromConst->mTypeCode == BfTypeCode_Double))
|
else if ((fromConst->mTypeCode == BfTypeCode_Single) || (fromConst->mTypeCode == BfTypeCode_Double))
|
||||||
{
|
{
|
||||||
return CreateConst(fromConst->mTypeCode, fromConst->mDouble);
|
return CreateConst(fromConst->mTypeCode, fromConst->mDouble);
|
||||||
}
|
}
|
||||||
else if (fromConst->mTypeCode == BfTypeCode_NullPtr)
|
else if (fromConst->mTypeCode == BfTypeCode_NullPtr)
|
||||||
|
|
|
@ -81,8 +81,9 @@ enum BfTypeCode : uint8
|
||||||
{
|
{
|
||||||
BfTypeCode_None,
|
BfTypeCode_None,
|
||||||
BfTypeCode_CharPtr,
|
BfTypeCode_CharPtr,
|
||||||
|
BfTypeCode_StringId,
|
||||||
BfTypeCode_Pointer,
|
BfTypeCode_Pointer,
|
||||||
BfTypeCode_NullPtr,
|
BfTypeCode_NullPtr,
|
||||||
BfTypeCode_Self,
|
BfTypeCode_Self,
|
||||||
BfTypeCode_Dot,
|
BfTypeCode_Dot,
|
||||||
BfTypeCode_Var,
|
BfTypeCode_Var,
|
||||||
|
|
|
@ -1518,7 +1518,7 @@ int BfModule::GetStringPoolIdx(BfIRValue constantStr, BfIRConstHolder* constHold
|
||||||
constHolder = mBfIRBuilder;
|
constHolder = mBfIRBuilder;
|
||||||
|
|
||||||
auto constant = constHolder->GetConstant(constantStr);
|
auto constant = constHolder->GetConstant(constantStr);
|
||||||
if (constant->mTypeCode == BfTypeCode_Int32)
|
if (constant->mTypeCode == BfTypeCode_StringId)
|
||||||
{
|
{
|
||||||
return constant->mInt32;
|
return constant->mInt32;
|
||||||
}
|
}
|
||||||
|
@ -6075,7 +6075,7 @@ BfIRFunction BfModule::GetIntrinsic(BfMethodInstance* methodInstance, bool repor
|
||||||
auto constant = methodOwner->mConstHolder->GetConstant(customAttribute.mCtorArgs[0]);
|
auto constant = methodOwner->mConstHolder->GetConstant(customAttribute.mCtorArgs[0]);
|
||||||
String error;
|
String error;
|
||||||
|
|
||||||
if ((constant != NULL) && (BfIRConstHolder::IsInt(constant->mTypeCode)))
|
if ((constant != NULL) && (constant->mTypeCode = BfTypeCode_StringId))
|
||||||
{
|
{
|
||||||
int stringId = constant->mInt32;
|
int stringId = constant->mInt32;
|
||||||
auto entry = mContext->mStringObjectIdMap[stringId];
|
auto entry = mContext->mStringObjectIdMap[stringId];
|
||||||
|
@ -9335,7 +9335,7 @@ void BfModule::CurrentAddToConstHolder(BfIRValue& irVal)
|
||||||
int stringPoolIdx = GetStringPoolIdx(irVal, mBfIRBuilder);
|
int stringPoolIdx = GetStringPoolIdx(irVal, mBfIRBuilder);
|
||||||
if (stringPoolIdx != -1)
|
if (stringPoolIdx != -1)
|
||||||
{
|
{
|
||||||
irVal = mCurTypeInstance->GetOrCreateConstHolder()->CreateConst(BfTypeCode_Int32, stringPoolIdx);
|
irVal = mCurTypeInstance->GetOrCreateConstHolder()->CreateConst(BfTypeCode_StringId, stringPoolIdx);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9374,56 +9374,70 @@ void BfModule::ClearConstData()
|
||||||
}
|
}
|
||||||
|
|
||||||
BfTypedValue BfModule::GetTypedValueFromConstant(BfConstant* constant, BfIRConstHolder* constHolder, BfType* wantType)
|
BfTypedValue BfModule::GetTypedValueFromConstant(BfConstant* constant, BfIRConstHolder* constHolder, BfType* wantType)
|
||||||
{
|
{
|
||||||
|
switch (constant->mTypeCode)
|
||||||
bool isString = false;
|
|
||||||
isString = (wantType->IsObject()) && (wantType->ToTypeInstance()->mTypeDef == mCompiler->mStringTypeDef);
|
|
||||||
if (wantType->IsPointer())
|
|
||||||
isString = true;
|
|
||||||
|
|
||||||
if (!isString)
|
|
||||||
{
|
{
|
||||||
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:
|
auto constVal = mBfIRBuilder->CreateConst(constant, constHolder);
|
||||||
case BfTypeCode_Int8:
|
BfTypedValue typedValue;
|
||||||
case BfTypeCode_UInt8:
|
|
||||||
case BfTypeCode_Int16:
|
if (constant->mTypeCode == BfTypeCode_StringId)
|
||||||
case BfTypeCode_UInt16:
|
{
|
||||||
case BfTypeCode_Int32:
|
if ((wantType->IsInstanceOf(mCompiler->mStringTypeDef)) ||
|
||||||
case BfTypeCode_UInt32:
|
((wantType->IsPointer()) && (wantType->GetUnderlyingType() == GetPrimitiveType(BfTypeCode_Char8))))
|
||||||
case BfTypeCode_Int64:
|
{
|
||||||
case BfTypeCode_UInt64:
|
typedValue = BfTypedValue(ConstantToCurrent(constant, constHolder, wantType), wantType);
|
||||||
case BfTypeCode_IntPtr:
|
return typedValue;
|
||||||
case BfTypeCode_UIntPtr:
|
}
|
||||||
case BfTypeCode_IntUnknown:
|
|
||||||
case BfTypeCode_UIntUnknown:
|
auto stringType = ResolveTypeDef(mCompiler->mStringTypeDef);
|
||||||
case BfTypeCode_Char8:
|
typedValue = BfTypedValue(ConstantToCurrent(constant, constHolder, stringType), stringType);
|
||||||
case BfTypeCode_Char16:
|
}
|
||||||
case BfTypeCode_Char32:
|
|
||||||
case BfTypeCode_Single:
|
if (!typedValue)
|
||||||
case BfTypeCode_Double:
|
|
||||||
{
|
{
|
||||||
auto constVal = mBfIRBuilder->CreateConst(constant, constHolder);
|
auto constVal = mBfIRBuilder->CreateConst(constant, constHolder);
|
||||||
auto typedValue = BfTypedValue(constVal, GetPrimitiveType(constant->mTypeCode));
|
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;
|
|
||||||
}
|
}
|
||||||
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);
|
BfIRValue irValue = ConstantToCurrent(constant, constHolder, wantType);
|
||||||
BF_ASSERT(irValue);
|
BF_ASSERT(irValue);
|
||||||
if (!irValue)
|
if (!irValue)
|
||||||
|
@ -9438,17 +9452,19 @@ BfIRValue BfModule::ConstantToCurrent(BfConstant* constant, BfIRConstHolder* con
|
||||||
return GetDefaultValue(wantType);
|
return GetDefaultValue(wantType);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isString = false;
|
if (constant->mTypeCode == BfTypeCode_StringId)
|
||||||
isString = (wantType->IsObject()) && (wantType->ToTypeInstance()->mTypeDef == mCompiler->mStringTypeDef);
|
{
|
||||||
if (((wantType->IsPointer()) || (isString)) && (mBfIRBuilder->IsInt(constant->mTypeCode)))
|
if ((wantType->IsInstanceOf(mCompiler->mStringTypeDef)) ||
|
||||||
{
|
((wantType->IsPointer()) && (wantType->GetUnderlyingType() == GetPrimitiveType(BfTypeCode_Char8))))
|
||||||
const StringImpl& str = mContext->mStringObjectIdMap[constant->mInt32].mString;
|
{
|
||||||
BfIRValue stringObjConst = GetStringObjectValue(str);
|
const StringImpl& str = mContext->mStringObjectIdMap[constant->mInt32].mString;
|
||||||
if (wantType->IsObject())
|
BfIRValue stringObjConst = GetStringObjectValue(str);
|
||||||
return stringObjConst;
|
if (wantType->IsPointer())
|
||||||
return GetStringCharPtr(stringObjConst);
|
return GetStringCharPtr(stringObjConst);
|
||||||
}
|
return stringObjConst;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (constant->mConstType == BfConstType_Array)
|
if (constant->mConstType == BfConstType_Array)
|
||||||
{
|
{
|
||||||
auto elementType = wantType->GetUnderlyingType();
|
auto elementType = wantType->GetUnderlyingType();
|
||||||
|
|
|
@ -4536,6 +4536,9 @@ BfPrimitiveType* BfModule::GetPrimitiveType(BfTypeCode typeCode)
|
||||||
case BfTypeCode_UIntUnknown:
|
case BfTypeCode_UIntUnknown:
|
||||||
primType = (BfPrimitiveType*)ResolveTypeDef(mSystem->mTypeUIntUnknown);
|
primType = (BfPrimitiveType*)ResolveTypeDef(mSystem->mTypeUIntUnknown);
|
||||||
break;
|
break;
|
||||||
|
case BfTypeCode_StringId:
|
||||||
|
BF_FATAL("Invalid use of StringId");
|
||||||
|
break;
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
mContext->mPrimitiveTypes[typeCode] = primType;
|
mContext->mPrimitiveTypes[typeCode] = primType;
|
||||||
|
|
|
@ -231,6 +231,13 @@ namespace Tests
|
||||||
{
|
{
|
||||||
return val.mA;
|
return val.mA;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static implicit operator Self(int val)
|
||||||
|
{
|
||||||
|
IntStruct sVal;
|
||||||
|
sVal.mA = val;
|
||||||
|
return sVal;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
@ -244,6 +251,12 @@ namespace Tests
|
||||||
const String cStrD = "D";
|
const String cStrD = "D";
|
||||||
const char8* cStrPD = "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]
|
[Test]
|
||||||
public static void TestStringOp()
|
public static void TestStringOp()
|
||||||
{
|
{
|
||||||
|
@ -254,6 +267,8 @@ namespace Tests
|
||||||
const char8* cStr3 = "A" + "B";
|
const char8* cStr3 = "A" + "B";
|
||||||
const char8* cStr4 = cStr1 + "C" + cStrPD;
|
const char8* cStr4 = cStr1 + "C" + cStrPD;
|
||||||
Test.Assert(StringView(cStr4) == "ABCD");
|
Test.Assert(StringView(cStr4) == "ABCD");
|
||||||
|
|
||||||
|
TestDefaults();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue