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

Added support for const string generic args

This commit is contained in:
Brian Fiete 2022-02-04 14:26:50 -05:00
parent 361be9dc92
commit a87ccd299d
5 changed files with 63 additions and 5 deletions

View file

@ -3723,6 +3723,9 @@ void BfExprEvaluator::GetLiteral(BfAstNode* refNode, const BfVariant& variant)
case BfTypeCode_Double:
mResult = BfTypedValue(mModule->mBfIRBuilder->CreateConst(variant.mTypeCode, variant.mDouble), mModule->GetPrimitiveType(variant.mTypeCode));
break;
case BfTypeCode_StringId:
mResult = BfTypedValue(mModule->mBfIRBuilder->CreateConst(variant.mTypeCode, variant.mUInt64), mModule->ResolveTypeDef(mModule->mCompiler->mStringTypeDef));
break;
default:
mModule->Fail("Invalid literal", refNode);
break;

View file

@ -1639,14 +1639,13 @@ BfIRValue BfModule::CreateStringObjectValue(const StringImpl& str, int stringId,
mBfIRBuilder->PopulateType(stringTypeInst);
auto stringValLiteral = mBfIRBuilder->CreateGlobalVariable(
mBfIRBuilder->MapTypeInst(stringTypeInst),
mBfIRBuilder->MapTypeInst(stringTypeInst, BfIRPopulateType_Full),
true,
BfIRLinkageType_External,
define ? stringValData : BfIRValue(),
stringObjName);
auto stringVal = mBfIRBuilder->CreateBitCast(stringValLiteral, mBfIRBuilder->MapType(stringTypeInst, BfIRPopulateType_Full));
return stringVal;
return stringValLiteral;
}
int BfModule::GetStringPoolIdx(BfIRValue constantStr, BfIRConstHolder* constHolder)
@ -7666,6 +7665,9 @@ void BfModule::ResolveGenericParamConstraints(BfGenericParamInstance* genericPar
if (constraintType->IsPrimitiveType())
typeCode = ((BfPrimitiveType*)constraintType)->mTypeDef->mTypeCode;
if (constraintType->IsInstanceOf(mCompiler->mStringTypeDef))
isValidTypeCode = true;
switch (typeCode)
{
case BfTypeCode_StringId:
@ -12070,6 +12072,8 @@ BfVariant BfModule::TypedValueToVariant(BfAstNode* refNode, const BfTypedValue&
primType = (BfPrimitiveType*)value.mType;
else if (value.mType->IsTypedPrimitive())
primType = value.mType->GetUnderlyingType();
else if (value.mType->IsInstanceOf(mCompiler->mStringTypeDef))
primType = value.mType;
if (primType)
{
@ -12083,6 +12087,17 @@ BfVariant BfModule::TypedValueToVariant(BfAstNode* refNode, const BfTypedValue&
return variant;
}
if (constant->mConstType == BfConstType_GlobalVar)
{
int stringIdx = GetStringPoolIdx(value.mValue, mBfIRBuilder);
if (stringIdx != -1)
{
variant.mTypeCode = BfTypeCode_StringId;
variant.mInt64 = stringIdx;
return variant;
}
}
switch (constant->mTypeCode)
{
case BfTypeCode_Boolean:
@ -12102,6 +12117,7 @@ BfVariant BfModule::TypedValueToVariant(BfAstNode* refNode, const BfTypedValue&
case BfTypeCode_Char8:
case BfTypeCode_Char16:
case BfTypeCode_Char32:
case BfTypeCode_StringId:
variant.mTypeCode = constant->mTypeCode;
variant.mInt64 = constant->mInt64;
break;

View file

@ -12338,6 +12338,13 @@ BfIRValue BfModule::CastToValue(BfAstNode* srcNode, BfTypedValue typedVal, BfTyp
return typedVal.mValue;
}
if (toConstExprValueType->mValue.mTypeCode == BfTypeCode_StringId)
{
int stringIdx = GetStringPoolIdx(typedVal.mValue, mBfIRBuilder);
if ((stringIdx != -1) && (stringIdx == toConstExprValueType->mValue.mInt32))
return typedVal.mValue;
}
if (!ignoreErrors)
{
String valStr;
@ -13786,6 +13793,15 @@ void BfModule::VariantToString(StringImpl& str, const BfVariant& variant)
str += ".0";
}
break;
case BfTypeCode_StringId:
{
int stringId = variant.mInt32;
auto stringPoolEntry = mContext->mStringObjectIdMap[stringId];
str += '"';
str += SlashString(stringPoolEntry.mString, false, false, true);
str += '"';
}
break;
case BfTypeCode_Let:
str += "?";
break;

View file

@ -1617,8 +1617,8 @@ BfLocalVariable* BfModule::HandleVariableDeclaration(BfVariableDeclaration* varD
// isConst = false;
// initHandled = true;
// localDef->mValue = initValue.mValue;
isConst = false;
if (GetStringPoolIdx(initValue.mValue, mBfIRBuilder) == -1)
isConst = false;
}
}
};

View file

@ -433,6 +433,24 @@ namespace Tests
return a;
}
static Type GetMathRetType<T, T2>()
{
return typeof(T);
}
static String GetMathString<T, T2>(String expr)
{
if ((typeof(T) == typeof(float)) && (typeof(T2) == typeof(int)))
return scope $"return a{expr}(T)b;";
else
return "return default;";
}
static comptype(GetMathRetType<T, T2>()) ComputeMath<T, T2, TExpr>(T a, T2 b, TExpr expr) where TExpr : const String
{
Compiler.Mixin(GetMathString<T, T2>(expr));
}
[Test]
public static void TestBasics()
{
@ -506,6 +524,11 @@ namespace Tests
int b = 34;
Test.Assert(GetMixinVal() == 123);
Test.Assert(b == 234);
float math = ComputeMath(2.3f, 2, "*");
Test.Assert(math == 4.6f);
float math2 = ComputeMath<float, int, const "+">(2.3f, 1, "+");
Test.Assert(math2 == 3.3f);
}
}
}