1
0
Fork 0
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:
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->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) 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

View file

@ -573,7 +573,7 @@ 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);
} }

View file

@ -81,6 +81,7 @@ 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,

View file

@ -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;
} }
@ -9375,16 +9375,9 @@ void BfModule::ClearConstData()
BfTypedValue BfModule::GetTypedValueFromConstant(BfConstant* constant, BfIRConstHolder* constHolder, BfType* wantType) 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_Boolean:
case BfTypeCode_Int8: case BfTypeCode_Int8:
case BfTypeCode_UInt8: case BfTypeCode_UInt8:
@ -9405,7 +9398,27 @@ BfTypedValue BfModule::GetTypedValueFromConstant(BfConstant* constant, BfIRConst
case BfTypeCode_Double: case BfTypeCode_Double:
{ {
auto constVal = mBfIRBuilder->CreateConst(constant, constHolder); auto constVal = mBfIRBuilder->CreateConst(constant, constHolder);
auto typedValue = BfTypedValue(constVal, GetPrimitiveType(constant->mTypeCode)); 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);
typedValue = BfTypedValue(constVal, GetPrimitiveType(constant->mTypeCode));
}
if (typedValue.mType == wantType) if (typedValue.mType == wantType)
return typedValue; return typedValue;
if (wantType->IsTypedPrimitive()) if (wantType->IsTypedPrimitive())
@ -9417,13 +9430,14 @@ BfTypedValue BfModule::GetTypedValueFromConstant(BfConstant* constant, BfIRConst
} }
} }
auto castedTypedValue = Cast(NULL, typedValue, wantType, (BfCastFlags)(BfCastFlags_SilentFail | BfCastFlags_Explicit)); auto castedTypedValue = Cast(NULL, typedValue, wantType, (BfCastFlags)(BfCastFlags_SilentFail | BfCastFlags_Explicit));
BF_ASSERT(castedTypedValue); if (!castedTypedValue)
return BfTypedValue();
return castedTypedValue; return castedTypedValue;
} }
break; break;
default: 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,15 +9452,17 @@ 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; const StringImpl& str = mContext->mStringObjectIdMap[constant->mInt32].mString;
BfIRValue stringObjConst = GetStringObjectValue(str); BfIRValue stringObjConst = GetStringObjectValue(str);
if (wantType->IsObject()) if (wantType->IsPointer())
return stringObjConst;
return GetStringCharPtr(stringObjConst); return GetStringCharPtr(stringObjConst);
return stringObjConst;
}
} }
if (constant->mConstType == BfConstType_Array) if (constant->mConstType == BfConstType_Array)

View file

@ -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;

View file

@ -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();
} }
} }
} }