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

Allow CreateObject for default ctors with append allocs

This commit is contained in:
Brian Fiete 2022-01-29 09:57:43 -05:00
parent aba01b2cc8
commit 3338f3c069
5 changed files with 61 additions and 14 deletions

View file

@ -121,32 +121,55 @@ namespace System.Reflection
return .Err; return .Err;
MethodInfo methodInfo = default; MethodInfo methodInfo = default;
MethodInfo calcAppendMethodInfo = default;
if (!IsBoxed) if (!IsBoxed)
{ {
for (int methodId < mMethodDataCount) for (int methodId < mMethodDataCount)
{ {
let methodData = &mMethodDataPtr[methodId]; let methodData = &mMethodDataPtr[methodId];
if ((!methodData.mFlags.HasFlag(.Constructor)) || (methodData.mFlags.HasFlag(.Static))) if ((!methodData.mFlags.HasFlag(.Constructor)) || (methodData.mFlags.HasFlag(.Static)))
{
if (((Object)methodData.mName == "this$calcAppend") && (methodData.mParamCount == 0))
calcAppendMethodInfo = .(this, methodData);
continue; continue;
if (methodData.mParamCount != 0) }
continue; if (methodData.mParamCount == 0)
{
methodInfo = .(this, methodData); methodInfo = .(this, methodData);
break; break;
}
else if ((methodData.mParamCount == 1) && (methodData.mParamData[0].mParamFlags.HasFlag(.AppendIdx)))
methodInfo = .(this, methodData);
} }
if (!methodInfo.IsInitialized) if (!methodInfo.IsInitialized)
return .Err; return .Err;
if ((methodInfo.[Friend]mMethodData.mParamCount != 0) && (!calcAppendMethodInfo.IsInitialized))
return .Err;
} }
Object obj; Object obj;
let objType = typeof(Object) as TypeInstance; let objType = typeof(Object) as TypeInstance;
int allocSize = mInstSize;
bool hasAppendAlloc = (methodInfo.IsInitialized) && (methodInfo.[Friend]mMethodData.mParamCount != 0);
if (hasAppendAlloc)
{
switch (calcAppendMethodInfo.Invoke(null))
{
case .Err:
return .Err;
case .Ok(let val):
allocSize += val.Get<int>();
}
}
#if BF_ENABLE_OBJECT_DEBUG_FLAGS #if BF_ENABLE_OBJECT_DEBUG_FLAGS
int32 stackCount = Compiler.Options.AllocStackCount; int32 stackCount = Compiler.Options.AllocStackCount;
if (mAllocStackCountOverride != 0) if (mAllocStackCountOverride != 0)
stackCount = mAllocStackCountOverride; stackCount = mAllocStackCountOverride;
obj = Internal.Dbg_ObjectAlloc(mTypeClassVData, mInstSize, mInstAlign, stackCount); obj = Internal.Dbg_ObjectAlloc(mTypeClassVData, allocSize, mInstAlign, stackCount);
#else #else
void* mem = new [Align(16)] uint8[mInstSize]* (?); void* mem = new [Align(16)] uint8[mInstSize]* (?);
obj = Internal.UnsafeCastToObject(mem); obj = Internal.UnsafeCastToObject(mem);
@ -155,7 +178,13 @@ namespace System.Reflection
Internal.MemSet((uint8*)Internal.UnsafeCastToPtr(obj) + objType.mInstSize, 0, mInstSize - objType.mInstSize); Internal.MemSet((uint8*)Internal.UnsafeCastToPtr(obj) + objType.mInstSize, 0, mInstSize - objType.mInstSize);
if (methodInfo.IsInitialized) if (methodInfo.IsInitialized)
{ {
if (methodInfo.Invoke(obj) case .Err) Object[] args = null;
if (hasAppendAlloc)
args = scope:: .(scope:: box ((int)Internal.UnsafeCastToPtr(obj) + mInstSize));
else
args = scope:: Object[0];
if (methodInfo.Invoke(obj, params args) case .Err)
{ {
delete obj; delete obj;
return .Err; return .Err;

View file

@ -756,7 +756,8 @@ namespace System.Reflection
{ {
None = 0, None = 0,
Splat = 1, Splat = 1,
Implicit = 2 Implicit = 2,
AppendIdx = 4
} }
[CRepr, AlwaysInclude] [CRepr, AlwaysInclude]

View file

@ -6831,11 +6831,11 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
if ((methodDef->mIsStatic) && ((methodReflectKind & BfReflectKind_StaticMethods) != 0)) if ((methodDef->mIsStatic) && ((methodReflectKind & BfReflectKind_StaticMethods) != 0))
includeMethod = true; includeMethod = true;
if (methodDef->mMethodType == BfMethodType_Ctor) if ((methodDef->mMethodType == BfMethodType_Ctor) || (methodDef->mMethodType == BfMethodType_CtorCalcAppend))
{ {
if ((methodReflectKind & BfReflectKind_Constructors) != 0) if ((methodReflectKind & BfReflectKind_Constructors) != 0)
includeMethod = true; includeMethod = true;
if ((methodDef->mParams.IsEmpty()) && ((methodReflectKind & BfReflectKind_DefaultConstructor) != 0)) if ((methodDef->IsDefaultCtor()) && ((methodReflectKind & BfReflectKind_DefaultConstructor) != 0))
includeMethod = true; includeMethod = true;
} }
@ -6906,6 +6906,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
ParamFlag_None = 0, ParamFlag_None = 0,
ParamFlag_Splat = 1, ParamFlag_Splat = 1,
ParamFlag_Implicit = 2, ParamFlag_Implicit = 2,
ParamFlag_AppendIdx = 4
}; };
SizedArray<BfIRValue, 8> paramVals; SizedArray<BfIRValue, 8> paramVals;
@ -6917,6 +6918,8 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
ParamFlags paramFlags = ParamFlag_None; ParamFlags paramFlags = ParamFlag_None;
if (defaultMethod->GetParamIsSplat(paramIdx)) if (defaultMethod->GetParamIsSplat(paramIdx))
paramFlags = (ParamFlags)(paramFlags | ParamFlag_Splat); paramFlags = (ParamFlags)(paramFlags | ParamFlag_Splat);
if (defaultMethod->GetParamKind(paramIdx) == BfParamKind_AppendIdx)
paramFlags = (ParamFlags)(paramFlags | ParamFlag_Implicit | ParamFlag_AppendIdx);
BfIRValue paramNameConst = GetStringObjectValue(paramName, !mIsComptimeModule); BfIRValue paramNameConst = GetStringObjectValue(paramName, !mIsComptimeModule);
@ -10109,6 +10112,12 @@ BfTypedValue BfModule::BoxValue(BfAstNode* srcNode, BfTypedValue typedVal, BfTyp
if ((!typedVal.mType->IsPointer()) || (toTypeInstance == mContext->mBfObjectType)) if ((!typedVal.mType->IsPointer()) || (toTypeInstance == mContext->mBfObjectType))
fromStructTypeInstance = GetWrappedStructType(typedVal.mType); fromStructTypeInstance = GetWrappedStructType(typedVal.mType);
else
{
auto checkStructTypeInstance = GetWrappedStructType(typedVal.mType);
if (checkStructTypeInstance == toType->GetUnderlyingType())
fromStructTypeInstance = checkStructTypeInstance;
}
if (isStructPtr) if (isStructPtr)
{ {

View file

@ -5536,9 +5536,10 @@ void BfModule::DoTypeInstanceMethodProcessing(BfTypeInstance* typeInstance)
} }
if (typeInstance->IncludeAllMethods()) if (typeInstance->IncludeAllMethods())
implRequired = true; implRequired = true;
// "AssumeInstantiated" also forces default ctor // "AssumeInstantiated" also forces default ctor
if (((typeInstance->mAlwaysIncludeFlags & BfAlwaysIncludeFlag_AssumeInstantiated) != 0) && if (((typeInstance->mAlwaysIncludeFlags & BfAlwaysIncludeFlag_AssumeInstantiated) != 0) &&
(methodDef->mMethodType == BfMethodType_Ctor) && (methodDef->mParams.IsEmpty())) (methodDef->IsDefaultCtor()))
implRequired = true; implRequired = true;
if ((typeOptionsIncludeAll) && (ApplyTypeOptionMethodFilters(true, methodDef, typeOptions))) if ((typeOptionsIncludeAll) && (ApplyTypeOptionMethodFilters(true, methodDef, typeOptions)))

View file

@ -550,7 +550,14 @@ bool BfMethodDef::IsEmptyPartial()
bool BfMethodDef::IsDefaultCtor() bool BfMethodDef::IsDefaultCtor()
{ {
return ((mMethodType == BfMethodType_Ctor) || (mMethodType == BfMethodType_CtorNoBody)) && (mParams.IsEmpty()); if ((mMethodType == BfMethodType_Ctor) || (mMethodType == BfMethodType_CtorNoBody))
{
return ((mParams.IsEmpty()) ||
((mParams.mSize == 1) && (mParams[0]->mParamKind == BfParamKind_AppendIdx)));
}
else if (mMethodType == BfMethodType_CtorCalcAppend)
return mParams.IsEmpty();
return false;
} }
bool BfMethodDef::IsCtorOrInit() bool BfMethodDef::IsCtorOrInit()