mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-08 19:48:20 +02:00
Improved string-to-sized-array casts and initializations
This commit is contained in:
parent
1320b495d2
commit
c6f2798db7
4 changed files with 55 additions and 1 deletions
|
@ -14006,6 +14006,40 @@ BfIRValue BfModule::CastToValue(BfAstNode* srcNode, BfTypedValue typedVal, BfTyp
|
|||
}
|
||||
}
|
||||
}
|
||||
else if (toType->IsSizedArray())
|
||||
{
|
||||
auto sizedArray = (BfSizedArrayType*)toType;
|
||||
if (sizedArray->mElementType == GetPrimitiveType(BfTypeCode_Char8))
|
||||
{
|
||||
int stringId = GetStringPoolIdx(typedVal.mValue, mBfIRBuilder);
|
||||
if (stringId >= 0)
|
||||
{
|
||||
BfStringPoolEntry* entry = NULL;
|
||||
if (mContext->mStringObjectIdMap.TryGetValue(stringId, &entry))
|
||||
{
|
||||
String& string = entry->mString;
|
||||
|
||||
if (string.GetLength() > sizedArray->mElementCount)
|
||||
{
|
||||
if (!ignoreErrors)
|
||||
Fail(StrFormat("String literal is too long to fit into '%s'", TypeToString(sizedArray).c_str()), srcNode);
|
||||
}
|
||||
|
||||
Array<BfIRValue> charValues;
|
||||
for (int i = 0; i < (int)BF_MIN(string.GetLength(), sizedArray->mElementCount); i++)
|
||||
{
|
||||
char c = string[i];
|
||||
charValues.Add(mBfIRBuilder->CreateConst(BfTypeCode_Char8, (int)(uint8)c));
|
||||
}
|
||||
|
||||
if (sizedArray->mElementCount > charValues.size())
|
||||
charValues.Add(mBfIRBuilder->CreateConst(BfTypeCode_Char8, 0));
|
||||
|
||||
return mBfIRBuilder->CreateConstAgg(mBfIRBuilder->MapType(sizedArray), charValues);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check user-defined operators
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue