1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-09 03:52:19 +02:00

Reworked static append fields

This commit is contained in:
Brian Fiete 2025-01-24 06:14:46 -08:00
parent 9d8647cd84
commit ec34aa3d43
6 changed files with 147 additions and 85 deletions

View file

@ -5650,79 +5650,7 @@ void BfModule::DoPopulateType(BfType* resolvedTypeRef, BfPopulateType populateTy
if (fieldInstance->IsAppendedObject())
{
SetAndRestoreValue<BfFieldDef*> prevTypeRef(mContext->mCurTypeState->mCurFieldDef, fieldDef);
SetAndRestoreValue<BfTypeState::ResolveKind> prevResolveKind(mContext->mCurTypeState->mResolveKind, BfTypeState::ResolveKind_FieldType);
PopulateType(resolvedFieldType, BfPopulateType_Data);
auto fieldTypeInst = resolvedFieldType->ToTypeInstance();
dataSize = BF_MAX(fieldTypeInst->mInstSize, 0);
alignSize = BF_MAX(fieldTypeInst->mInstAlign, 1);
if (fieldTypeInst->mTypeFailed)
{
TypeFailed(fieldTypeInst);
fieldInstance->mResolvedType = GetPrimitiveType(BfTypeCode_Var);
continue;
}
if ((typeInstance != NULL) && (fieldTypeInst->mTypeDef->mIsAbstract))
{
Fail("Cannot create an instance of an abstract class", nameRefNode);
}
SetAndRestoreValue<bool> prevIgnoreWrites(mBfIRBuilder->mIgnoreWrites, true);
BfMethodState methodState;
SetAndRestoreValue<BfMethodState*> prevMethodState(mCurMethodState, &methodState);
methodState.mTempKind = BfMethodState::TempKind_NonStatic;
BfTypedValue appendIndexValue;
BfExprEvaluator exprEvaluator(this);
BfResolvedArgs resolvedArgs;
auto fieldDecl = fieldDef->GetFieldDeclaration();
if (auto invocationExpr = BfNodeDynCast<BfInvocationExpression>(fieldDecl->mInitializer))
{
resolvedArgs.Init(invocationExpr->mOpenParen, &invocationExpr->mArguments, &invocationExpr->mCommas, invocationExpr->mCloseParen);
exprEvaluator.ResolveArgValues(resolvedArgs, BfResolveArgsFlag_DeferParamEval);
}
BfFunctionBindResult bindResult;
bindResult.mSkipThis = true;
bindResult.mWantsArgs = true;
SetAndRestoreValue<BfFunctionBindResult*> prevBindResult(exprEvaluator.mFunctionBindResult, &bindResult);
BfTypedValue emptyThis(mBfIRBuilder->GetFakeVal(), resolvedTypeRef, resolvedTypeRef->IsStruct());
exprEvaluator.mBfEvalExprFlags = BfEvalExprFlags_Comptime;
auto ctorResult = exprEvaluator.MatchConstructor(nameRefNode, NULL, emptyThis, fieldTypeInst, resolvedArgs, false, BfMethodGenericArguments(), true);
if ((bindResult.mMethodInstance != NULL) && (bindResult.mMethodInstance->mMethodDef->mHasAppend))
{
auto calcAppendMethodModule = GetMethodInstanceAtIdx(bindResult.mMethodInstance->GetOwner(), bindResult.mMethodInstance->mMethodDef->mIdx + 1, BF_METHODNAME_CALCAPPEND);
SizedArray<BfIRValue, 2> irArgs;
if (bindResult.mIRArgs.size() > 1)
irArgs.Insert(0, &bindResult.mIRArgs[1], bindResult.mIRArgs.size() - 1);
BfTypedValue appendSizeTypedValue = TryConstCalcAppend(calcAppendMethodModule.mMethodInstance, irArgs, true);
if (appendSizeTypedValue)
{
int appendAlign = calcAppendMethodModule.mMethodInstance->mAppendAllocAlign;
dataSize = BF_ALIGN(dataSize, appendAlign);
alignSize = BF_MAX(alignSize, appendAlign);
auto constant = mBfIRBuilder->GetConstant(appendSizeTypedValue.mValue);
if (constant != NULL)
{
dataSize += constant->mInt32;
}
}
else
{
Fail(StrFormat("Append constructor '%s' does not result in a constant size", MethodToString(bindResult.mMethodInstance).c_str()), nameRefNode);
}
}
TryGetAppendedObjectInfo(fieldInstance, dataSize, alignSize);
}
else if (fieldDef->mIsAppend)
{