1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-07-04 23:36:00 +02:00

Comptime GetCustomAttribute for type/field/method

This commit is contained in:
Brian Fiete 2022-02-17 05:51:05 -05:00
parent 434a7406de
commit 4c5c89bab5
8 changed files with 110 additions and 13 deletions

View file

@ -2222,7 +2222,7 @@ void BfModule::HandleCEAttributes(CeEmitContext* ceEmitContext, BfTypeInstance*
SetAndRestoreValue<CeEmitContext*> prevEmitContext(mCompiler->mCEMachine->mCurEmitContext, ceEmitContext);
auto ceContext = mCompiler->mCEMachine->AllocContext();
BfIRValue attrVal = ceContext->CreateAttribute(customAttribute.mRef, this, typeInstance->mConstHolder, &customAttribute);
BfIRValue attrVal =ceContext->CreateAttribute(customAttribute.mRef, this, typeInstance->mConstHolder, &customAttribute);
for (int baseIdx = 0; baseIdx < checkDepth; baseIdx++)
attrVal = mBfIRBuilder->CreateExtractValue(attrVal, 0);

View file

@ -1422,6 +1422,7 @@ BfLocalVariable* BfModule::HandleVariableDeclaration(BfVariableDeclaration* varD
auto eqVal = mBfIRBuilder->CreateCmpEQ(dscVal, GetConstValue(tagId, dscType));
exprEvaluator->mResult = BfTypedValue(eqVal, boolType);
PopulateType(outType);
if (!outType->IsValuelessType())
{
auto outPtrType = CreatePointerType(outType);

View file

@ -3458,7 +3458,7 @@ bool CeContext::GetStringFromStringView(addr_ce addr, StringImpl& str)
return true;
}
bool CeContext::GetCustomAttribute(BfCustomAttributes* customAttributes, int attributeTypeId, addr_ce resultAddr)
bool CeContext::GetCustomAttribute(BfModule* module, BfIRConstHolder* constHolder, BfCustomAttributes* customAttributes, int attributeTypeId, addr_ce resultAddr)
{
if (customAttributes == NULL)
return false;
@ -3470,17 +3470,26 @@ bool CeContext::GetCustomAttribute(BfCustomAttributes* customAttributes, int att
auto customAttr = customAttributes->Get(attributeType);
if (customAttr == NULL)
return false;
if (resultAddr != 0)
auto ceContext = mCeMachine->AllocContext();
BfIRValue foreignValue = ceContext->CreateAttribute(mCurTargetSrc, module, constHolder, customAttr);
auto foreignConstant = module->mBfIRBuilder->GetConstant(foreignValue);
if (foreignConstant->mConstType == BfConstType_AggCE)
{
}
auto constAggData = (BfConstantAggCE*)foreignConstant;
auto value = ceContext->CreateConstant(module, ceContext->mMemory.mVals + constAggData->mCEAddr, customAttr->mType);
if (!value)
Fail("Failed to encoded attribute");
auto attrConstant = module->mBfIRBuilder->GetConstant(value);
if (!WriteConstant(module, resultAddr, attrConstant, customAttr->mType))
Fail("Failed to decode attribute");
}
mCeMachine->ReleaseContext(ceContext);
return true;
}
//#define CE_GETC(T) *((T*)(addr += sizeof(T)) - 1)
#define CE_GETC(T) *(T*)(mMemory.mVals + addr)
@ -4172,12 +4181,13 @@ BfIRValue CeContext::CreateConstant(BfModule* module, uint8* ptr, BfType* bfType
return BfIRValue();
}
BfIRValue CeContext::CreateAttribute(BfAstNode* targetSrc, BfModule* module, BfIRConstHolder* constHolder, BfCustomAttribute* customAttribute)
BfIRValue CeContext::CreateAttribute(BfAstNode* targetSrc, BfModule* module, BfIRConstHolder* constHolder, BfCustomAttribute* customAttribute, addr_ce ceAttrAddr)
{
SetAndRestoreValue<bool> prevIgnoreWrites(module->mBfIRBuilder->mIgnoreWrites, true);
module->mContext->mUnreifiedModule->PopulateType(customAttribute->mType);
auto ceAttrAddr = CeMalloc(customAttribute->mType->mSize) - mMemory.mVals;
if (ceAttrAddr == 0)
ceAttrAddr = CeMalloc(customAttribute->mType->mSize) - mMemory.mVals;
BfIRValue ceAttrVal = module->mBfIRBuilder->CreateConstAggCE(module->mBfIRBuilder->MapType(customAttribute->mType, BfIRPopulateType_Identity), ceAttrAddr);
BfTypedValue ceAttrTypedValue(ceAttrVal, customAttribute->mType);
@ -5115,11 +5125,52 @@ bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8*
{
auto typeInst = type->ToTypeInstance();
if (typeInst != NULL)
success = GetCustomAttribute(typeInst->mCustomAttributes, attributeTypeId, resultPtr);
success = GetCustomAttribute(mCurModule, typeInst->mConstHolder, typeInst->mCustomAttributes, attributeTypeId, resultPtr);
}
*(addr_ce*)(stackPtr + 0) = success;
}
else if (checkFunction->mFunctionKind == CeFunctionKind_Field_GetCustomAttribute)
{
int32 typeId = *(int32*)((uint8*)stackPtr + 1);
int32 fieldIdx = *(int32*)((uint8*)stackPtr + 1 + 4);
int32 attributeTypeId = *(int32*)((uint8*)stackPtr + 1 + 4 + 4);
addr_ce resultPtr = *(addr_ce*)((uint8*)stackPtr + 1 + 4 + 4 + 4);
BfType* type = GetBfType(typeId);
bool success = false;
if (type != NULL)
{
auto typeInst = type->ToTypeInstance();
if (typeInst != NULL)
{
if (typeInst->mDefineState < BfTypeDefineState_CETypeInit)
mCurModule->PopulateType(typeInst);
if (fieldIdx < typeInst->mFieldInstances.mSize)
{
auto& fieldInstance = typeInst->mFieldInstances[fieldIdx];
success = GetCustomAttribute(mCurModule, typeInst->mConstHolder, fieldInstance.mCustomAttributes, attributeTypeId, resultPtr);
}
}
}
*(addr_ce*)(stackPtr + 0) = success;
}
else if (checkFunction->mFunctionKind == CeFunctionKind_Method_GetCustomAttribute)
{
int64 methodHandle = *(int64*)((uint8*)stackPtr + 1);
int32 attributeTypeId = *(int32*)((uint8*)stackPtr + 1 + 8);
addr_ce resultPtr = *(addr_ce*)((uint8*)stackPtr + 1 + 8 + 4);
auto methodInstance = mCeMachine->GetMethodInstance(methodHandle);
if (methodInstance == NULL)
{
_Fail("Invalid method instance");
return false;
}
bool success = GetCustomAttribute(mCurModule, methodInstance->GetOwner()->mConstHolder, methodInstance->GetCustomAttributes(), attributeTypeId, resultPtr);
*(addr_ce*)(stackPtr + 0) = success;
}
else if (checkFunction->mFunctionKind == CeFunctionKind_GetMethodCount)
{
int32 typeId = *(int32*)((uint8*)stackPtr + 4);
@ -8070,6 +8121,14 @@ void CeMachine::CheckFunctionKind(CeFunction* ceFunction)
{
ceFunction->mFunctionKind = CeFunctionKind_Type_GetCustomAttribute;
}
else if (methodDef->mName == "Comptime_Field_GetCustomAttribute")
{
ceFunction->mFunctionKind = CeFunctionKind_Field_GetCustomAttribute;
}
else if (methodDef->mName == "Comptime_Method_GetCustomAttribute")
{
ceFunction->mFunctionKind = CeFunctionKind_Method_GetCustomAttribute;
}
else if (methodDef->mName == "Comptime_GetMethod")
{
ceFunction->mFunctionKind = CeFunctionKind_GetMethod;

View file

@ -326,6 +326,8 @@ enum CeFunctionKind
CeFunctionKind_GetReflectSpecializedType,
CeFunctionKind_Type_ToString,
CeFunctionKind_Type_GetCustomAttribute,
CeFunctionKind_Field_GetCustomAttribute,
CeFunctionKind_Method_GetCustomAttribute,
CeFunctionKind_GetMethodCount,
CeFunctionKind_GetMethod,
CeFunctionKind_Method_ToString,
@ -906,11 +908,11 @@ public:
bool CheckMemory(addr_ce addr, int32 size);
bool GetStringFromAddr(addr_ce strInstAddr, StringImpl& str);
bool GetStringFromStringView(addr_ce addr, StringImpl& str);
bool GetCustomAttribute(BfCustomAttributes* customAttributes, int attributeTypeId, addr_ce resultAddr);
bool GetCustomAttribute(BfModule* module, BfIRConstHolder* constHolder, BfCustomAttributes* customAttributes, int attributeTypeId, addr_ce resultAddr);
bool WriteConstant(BfModule* module, addr_ce addr, BfConstant* constant, BfType* type, bool isParams = false);
BfIRValue CreateConstant(BfModule* module, uint8* ptr, BfType* type, BfType** outType = NULL);
BfIRValue CreateAttribute(BfAstNode* targetSrc, BfModule* module, BfIRConstHolder* constHolder, BfCustomAttribute* customAttribute);
BfIRValue CreateAttribute(BfAstNode* targetSrc, BfModule* module, BfIRConstHolder* constHolder, BfCustomAttribute* customAttribute, addr_ce ceAttrAddr = 0);
bool Execute(CeFunction* startFunction, uint8* startStackPtr, uint8* startFramePtr, BfType*& returnType);
BfTypedValue Call(BfAstNode* targetSrc, BfModule* module, BfMethodInstance* methodInstance, const BfSizedArray<BfIRValue>& args, CeEvalFlags flags, BfType* expectingType);