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

Improved generic param reflection in comptime

This commit is contained in:
Brian Fiete 2022-01-31 15:41:05 -05:00
parent 157d3f90e5
commit 26506efc1e
9 changed files with 143 additions and 19 deletions

View file

@ -439,6 +439,7 @@ BfCompiler::BfCompiler(BfSystem* bfSystem, bool isResolveOnly)
mPointerTypeDef = NULL;
mReflectTypeIdTypeDef = NULL;
mReflectArrayType = NULL;
mReflectGenericParamType = NULL;
mReflectFieldDataDef = NULL;
mReflectFieldSplatDataDef = NULL;
mReflectMethodDataDef = NULL;
@ -1345,6 +1346,7 @@ void BfCompiler::CreateVData(BfVDataModule* bfModule)
reflectTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectSpecializedGenericType));
reflectTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectUnspecializedGenericType));
reflectTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectArrayType));
reflectTypeSet.Add(vdataContext->mUnreifiedModule->ResolveTypeDef(mReflectGenericParamType));
SmallVector<BfIRValue, 256> typeDataVector;
for (auto type : vdataTypeList)
@ -6835,6 +6837,7 @@ bool BfCompiler::DoCompile(const StringImpl& outputDirectory)
mPointerTypeDef = _GetRequiredType("System.Pointer", 0);
mReflectTypeIdTypeDef = _GetRequiredType("System.Reflection.TypeId");
mReflectArrayType = _GetRequiredType("System.Reflection.ArrayType");
mReflectGenericParamType = _GetRequiredType("System.Reflection.GenericParamType");
mReflectFieldDataDef = _GetRequiredType("System.Reflection.TypeInstance.FieldData");
mReflectFieldSplatDataDef = _GetRequiredType("System.Reflection.TypeInstance.FieldSplatData");
mReflectMethodDataDef = _GetRequiredType("System.Reflection.TypeInstance.MethodData");

View file

@ -398,6 +398,7 @@ public:
BfTypeDef* mPointerTypeDef;
BfTypeDef* mReflectTypeIdTypeDef;
BfTypeDef* mReflectArrayType;
BfTypeDef* mReflectGenericParamType;
BfTypeDef* mReflectFieldDataDef;
BfTypeDef* mReflectFieldSplatDataDef;
BfTypeDef* mReflectMethodDataDef;

View file

@ -10593,15 +10593,8 @@ void BfExprEvaluator::Visit(BfTypeOfExpression* typeOfExpr)
return;
}
if ((type->IsGenericParam()) && (!mModule->mIsComptimeModule))
{
mResult = BfTypedValue(mModule->mBfIRBuilder->GetUndefConstValue(mModule->mBfIRBuilder->MapType(typeType)), typeType);
}
else
{
mModule->AddDependency(type, mModule->mCurTypeInstance, BfDependencyMap::DependencyFlag_ExprTypeReference);
mResult = BfTypedValue(mModule->CreateTypeDataRef(type), typeType);
}
mModule->AddDependency(type, mModule->mCurTypeInstance, BfDependencyMap::DependencyFlag_ExprTypeReference);
mResult = BfTypedValue(mModule->CreateTypeDataRef(type), typeType);
}
bool BfExprEvaluator::LookupTypeProp(BfTypeOfExpression* typeOfExpr, BfIdentifierNode* propName)

View file

@ -5396,11 +5396,13 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
typeDataSource = ResolveTypeDef(mCompiler->mReflectSizedArrayType)->ToTypeInstance();
else if (type->IsConstExprValue())
typeDataSource = ResolveTypeDef(mCompiler->mReflectConstExprType)->ToTypeInstance();
else
typeDataSource = mContext->mBfTypeType;
if (type->IsGenericParam())
else if (type->IsGenericParam())
{
typeFlags |= BfTypeFlags_GenericParam;
typeDataSource = ResolveTypeDef(mCompiler->mReflectGenericParamType)->ToTypeInstance();
}
else
typeDataSource = mContext->mBfTypeType;
if ((!mTypeDataRefs.ContainsKey(typeDataSource)) && (typeDataSource != type))
{
@ -5636,6 +5638,22 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
mBfIRBuilder->GlobalVar_SetAlignment(typeDataVar, mSystem->mPtrSize);
typeDataVar = mBfIRBuilder->CreateBitCast(typeDataVar, mBfIRBuilder->MapType(mContext->mBfTypeType));
}
else if (type->IsGenericParam())
{
auto genericParamType = (BfGenericParamType*)type;
SizedArray<BfIRValue, 3> genericParamTypeDataParms =
{
typeData
};
auto reflectGenericParamType = ResolveTypeDef(mCompiler->mReflectGenericParamType)->ToTypeInstance();
FixConstValueParams(reflectGenericParamType, genericParamTypeDataParms);
auto genericParamTypeData = mBfIRBuilder->CreateConstAgg_Value(mBfIRBuilder->MapTypeInst(reflectGenericParamType, BfIRPopulateType_Full), genericParamTypeDataParms);
typeDataVar = mBfIRBuilder->CreateGlobalVariable(mBfIRBuilder->MapTypeInst(reflectGenericParamType), true,
BfIRLinkageType_External, genericParamTypeData, typeDataName);
mBfIRBuilder->GlobalVar_SetAlignment(typeDataVar, mSystem->mPtrSize);
typeDataVar = mBfIRBuilder->CreateBitCast(typeDataVar, mBfIRBuilder->MapType(mContext->mBfTypeType));
}
else
{
typeDataVar = mBfIRBuilder->CreateGlobalVariable(mBfIRBuilder->MapTypeInst(mContext->mBfTypeType), true,

View file

@ -2913,6 +2913,9 @@ CeContext::CeContext()
mCurFrame = NULL;
mCurModule = NULL;
mCurMethodInstance = NULL;
mCallerMethodInstance = NULL;
mCallerTypeInstance = NULL;
mCallerActiveTypeDef = NULL;
mCurExpectingType = NULL;
mCurEmitContext = NULL;
}
@ -2983,18 +2986,20 @@ BfError* CeContext::Fail(const CeFrame& curFrame, const StringImpl& str)
err += " ";
}
auto contextMethodInstance = mCurModule->mCurMethodInstance;
auto contextMethodInstance = mCallerMethodInstance;
auto contextTypeInstance = mCallerTypeInstance;
if (stackIdx > 1)
{
auto func = mCallStack[stackIdx - 1].mFunction;
contextMethodInstance = func->mCeFunctionInfo->mMethodInstance;
contextTypeInstance = contextMethodInstance->GetOwner();
}
err += StrFormat("in comptime ");
//
{
SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(mCeMachine->mCeModule->mCurTypeInstance, (contextMethodInstance != NULL) ? contextMethodInstance->GetOwner() : NULL);
SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(mCeMachine->mCeModule->mCurTypeInstance, contextTypeInstance);
SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(mCeMachine->mCeModule->mCurMethodInstance, contextMethodInstance);
if (ceFunction->mMethodInstance != NULL)
@ -3033,7 +3038,7 @@ BfError* CeContext::Fail(const CeFrame& curFrame, const StringImpl& str)
void CeContext::FixProjectRelativePath(StringImpl& path)
{
BfProject* activeProject = NULL;
auto activeTypeDef = mCurModule->GetActiveTypeDef();
auto activeTypeDef = mCallerActiveTypeDef;
if (activeTypeDef != NULL)
activeProject = activeTypeDef->mProject;
if (activeProject != NULL)
@ -3145,7 +3150,7 @@ addr_ce CeContext::GetReflectType(int typeId)
return *addrPtr;
auto ceModule = mCeMachine->mCeModule;
SetAndRestoreValue<bool> ignoreWrites(ceModule->mBfIRBuilder->mIgnoreWrites, false);
SetAndRestoreValue<bool> ignoreWrites(ceModule->mBfIRBuilder->mIgnoreWrites, false);
if (ceModule->mContext->mBfTypeType == NULL)
ceModule->mContext->ReflectInit();
@ -4064,8 +4069,14 @@ BfTypedValue CeContext::Call(BfAstNode* targetSrc, BfModule* module, BfMethodIns
SetAndRestoreValue<BfAstNode*> prevTargetSrc(mCurTargetSrc, targetSrc);
SetAndRestoreValue<BfModule*> prevModule(mCurModule, module);
SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(mCurMethodInstance, methodInstance);
SetAndRestoreValue<BfMethodInstance*> prevCallerMethodInstance(mCallerMethodInstance, module->mCurMethodInstance);
SetAndRestoreValue<BfTypeInstance*> prevCallerTypeInstance(mCallerTypeInstance, module->mCurTypeInstance);
SetAndRestoreValue<BfTypeDef*> prevCallerActiveTypeDef(mCallerActiveTypeDef, module->GetActiveTypeDef());
SetAndRestoreValue<BfType*> prevExpectingType(mCurExpectingType, expectingType);
SetAndRestoreValue<BfMethodInstance*> moduleCurMethodInstance(module->mCurMethodInstance, methodInstance);
SetAndRestoreValue<BfTypeInstance*> moduleCurTypeInstance(module->mCurTypeInstance, methodInstance->GetOwner());
SetAndRestoreValue<int> prevCurExecuteId(mCurModule->mCompiler->mCurCEExecuteId, mCeMachine->mExecuteId);
// Reentrancy may occur as methods need defining
@ -4854,6 +4865,23 @@ bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8*
_FixVariables();
CeSetAddrVal(stackPtr + 0, reflectType, ptrSize);
}
else if (checkFunction->mFunctionKind == CeFunctionKind_Type_ToString)
{
int32 typeId = *(int32*)((uint8*)stackPtr + ptrSize);
BfType* type = GetBfType(typeId);
bool success = false;
if (type == NULL)
{
_Fail("Invalid type");
return false;
}
SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(mCeMachine->mCeModule->mCurMethodInstance, mCallerMethodInstance);
SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(mCeMachine->mCeModule->mCurTypeInstance, mCallerTypeInstance);
CeSetAddrVal(stackPtr + 0, GetString(mCeMachine->mCeModule->TypeToString(type)), ptrSize);
_FixVariables();
}
else if (checkFunction->mFunctionKind == CeFunctionKind_Type_GetCustomAttribute)
{
int32 typeId = *(int32*)((uint8*)stackPtr + 1);
@ -7786,6 +7814,10 @@ void CeMachine::CheckFunctionKind(CeFunction* ceFunction)
{
ceFunction->mFunctionKind = CeFunctionKind_GetReflectSpecializedType;
}
else if (methodDef->mName == "Comptime_Type_ToString")
{
ceFunction->mFunctionKind = CeFunctionKind_Type_ToString;
}
else if (methodDef->mName == "Comptime_Type_GetCustomAttribute")
{
ceFunction->mFunctionKind = CeFunctionKind_Type_GetCustomAttribute;

View file

@ -324,6 +324,7 @@ enum CeFunctionKind
CeFunctionKind_GetReflectTypeById,
CeFunctionKind_GetReflectTypeByName,
CeFunctionKind_GetReflectSpecializedType,
CeFunctionKind_Type_ToString,
CeFunctionKind_Type_GetCustomAttribute,
CeFunctionKind_GetMethodCount,
CeFunctionKind_GetMethod,
@ -701,7 +702,7 @@ public:
mCurDbgLoc = NULL;
mFrameSize = 0;
}
void Fail(const StringImpl& error);
CeOperand FrameAlloc(BeType* type);
@ -867,6 +868,9 @@ public:
Dictionary<int, CeInternalData*> mInternalDataMap;
int mCurHandleId;
BfMethodInstance* mCallerMethodInstance;
BfTypeInstance* mCallerTypeInstance;
BfTypeDef* mCallerActiveTypeDef;
BfMethodInstance* mCurMethodInstance;
BfType* mCurExpectingType;
BfAstNode* mCurTargetSrc;
@ -877,7 +881,7 @@ public:
public:
CeContext();
~CeContext();
BfError* Fail(const StringImpl& error);
BfError* Fail(const CeFrame& curFrame, const StringImpl& error);