mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-10 04:22:20 +02:00
Added support for const string generic args
This commit is contained in:
parent
361be9dc92
commit
a87ccd299d
5 changed files with 63 additions and 5 deletions
|
@ -3723,6 +3723,9 @@ void BfExprEvaluator::GetLiteral(BfAstNode* refNode, const BfVariant& variant)
|
||||||
case BfTypeCode_Double:
|
case BfTypeCode_Double:
|
||||||
mResult = BfTypedValue(mModule->mBfIRBuilder->CreateConst(variant.mTypeCode, variant.mDouble), mModule->GetPrimitiveType(variant.mTypeCode));
|
mResult = BfTypedValue(mModule->mBfIRBuilder->CreateConst(variant.mTypeCode, variant.mDouble), mModule->GetPrimitiveType(variant.mTypeCode));
|
||||||
break;
|
break;
|
||||||
|
case BfTypeCode_StringId:
|
||||||
|
mResult = BfTypedValue(mModule->mBfIRBuilder->CreateConst(variant.mTypeCode, variant.mUInt64), mModule->ResolveTypeDef(mModule->mCompiler->mStringTypeDef));
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
mModule->Fail("Invalid literal", refNode);
|
mModule->Fail("Invalid literal", refNode);
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -1639,14 +1639,13 @@ BfIRValue BfModule::CreateStringObjectValue(const StringImpl& str, int stringId,
|
||||||
|
|
||||||
mBfIRBuilder->PopulateType(stringTypeInst);
|
mBfIRBuilder->PopulateType(stringTypeInst);
|
||||||
auto stringValLiteral = mBfIRBuilder->CreateGlobalVariable(
|
auto stringValLiteral = mBfIRBuilder->CreateGlobalVariable(
|
||||||
mBfIRBuilder->MapTypeInst(stringTypeInst),
|
mBfIRBuilder->MapTypeInst(stringTypeInst, BfIRPopulateType_Full),
|
||||||
true,
|
true,
|
||||||
BfIRLinkageType_External,
|
BfIRLinkageType_External,
|
||||||
define ? stringValData : BfIRValue(),
|
define ? stringValData : BfIRValue(),
|
||||||
stringObjName);
|
stringObjName);
|
||||||
|
|
||||||
auto stringVal = mBfIRBuilder->CreateBitCast(stringValLiteral, mBfIRBuilder->MapType(stringTypeInst, BfIRPopulateType_Full));
|
return stringValLiteral;
|
||||||
return stringVal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int BfModule::GetStringPoolIdx(BfIRValue constantStr, BfIRConstHolder* constHolder)
|
int BfModule::GetStringPoolIdx(BfIRValue constantStr, BfIRConstHolder* constHolder)
|
||||||
|
@ -7666,6 +7665,9 @@ void BfModule::ResolveGenericParamConstraints(BfGenericParamInstance* genericPar
|
||||||
if (constraintType->IsPrimitiveType())
|
if (constraintType->IsPrimitiveType())
|
||||||
typeCode = ((BfPrimitiveType*)constraintType)->mTypeDef->mTypeCode;
|
typeCode = ((BfPrimitiveType*)constraintType)->mTypeDef->mTypeCode;
|
||||||
|
|
||||||
|
if (constraintType->IsInstanceOf(mCompiler->mStringTypeDef))
|
||||||
|
isValidTypeCode = true;
|
||||||
|
|
||||||
switch (typeCode)
|
switch (typeCode)
|
||||||
{
|
{
|
||||||
case BfTypeCode_StringId:
|
case BfTypeCode_StringId:
|
||||||
|
@ -12070,6 +12072,8 @@ BfVariant BfModule::TypedValueToVariant(BfAstNode* refNode, const BfTypedValue&
|
||||||
primType = (BfPrimitiveType*)value.mType;
|
primType = (BfPrimitiveType*)value.mType;
|
||||||
else if (value.mType->IsTypedPrimitive())
|
else if (value.mType->IsTypedPrimitive())
|
||||||
primType = value.mType->GetUnderlyingType();
|
primType = value.mType->GetUnderlyingType();
|
||||||
|
else if (value.mType->IsInstanceOf(mCompiler->mStringTypeDef))
|
||||||
|
primType = value.mType;
|
||||||
|
|
||||||
if (primType)
|
if (primType)
|
||||||
{
|
{
|
||||||
|
@ -12083,6 +12087,17 @@ BfVariant BfModule::TypedValueToVariant(BfAstNode* refNode, const BfTypedValue&
|
||||||
return variant;
|
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)
|
switch (constant->mTypeCode)
|
||||||
{
|
{
|
||||||
case BfTypeCode_Boolean:
|
case BfTypeCode_Boolean:
|
||||||
|
@ -12102,6 +12117,7 @@ BfVariant BfModule::TypedValueToVariant(BfAstNode* refNode, const BfTypedValue&
|
||||||
case BfTypeCode_Char8:
|
case BfTypeCode_Char8:
|
||||||
case BfTypeCode_Char16:
|
case BfTypeCode_Char16:
|
||||||
case BfTypeCode_Char32:
|
case BfTypeCode_Char32:
|
||||||
|
case BfTypeCode_StringId:
|
||||||
variant.mTypeCode = constant->mTypeCode;
|
variant.mTypeCode = constant->mTypeCode;
|
||||||
variant.mInt64 = constant->mInt64;
|
variant.mInt64 = constant->mInt64;
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -12338,6 +12338,13 @@ BfIRValue BfModule::CastToValue(BfAstNode* srcNode, BfTypedValue typedVal, BfTyp
|
||||||
return typedVal.mValue;
|
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)
|
if (!ignoreErrors)
|
||||||
{
|
{
|
||||||
String valStr;
|
String valStr;
|
||||||
|
@ -13786,6 +13793,15 @@ void BfModule::VariantToString(StringImpl& str, const BfVariant& variant)
|
||||||
str += ".0";
|
str += ".0";
|
||||||
}
|
}
|
||||||
break;
|
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:
|
case BfTypeCode_Let:
|
||||||
str += "?";
|
str += "?";
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -1617,8 +1617,8 @@ BfLocalVariable* BfModule::HandleVariableDeclaration(BfVariableDeclaration* varD
|
||||||
// isConst = false;
|
// isConst = false;
|
||||||
// initHandled = true;
|
// initHandled = true;
|
||||||
// localDef->mValue = initValue.mValue;
|
// localDef->mValue = initValue.mValue;
|
||||||
|
if (GetStringPoolIdx(initValue.mValue, mBfIRBuilder) == -1)
|
||||||
isConst = false;
|
isConst = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -433,6 +433,24 @@ namespace Tests
|
||||||
return a;
|
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]
|
[Test]
|
||||||
public static void TestBasics()
|
public static void TestBasics()
|
||||||
{
|
{
|
||||||
|
@ -506,6 +524,11 @@ namespace Tests
|
||||||
int b = 34;
|
int b = 34;
|
||||||
Test.Assert(GetMixinVal() == 123);
|
Test.Assert(GetMixinVal() == 123);
|
||||||
Test.Assert(b == 234);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue