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

Merge pull request #1474 from disarray2077/gca

Add `GetCustomAttributes` and `GetCustomAttributes<T>`
This commit is contained in:
Brian Fiete 2022-03-01 18:53:59 +01:00 committed by GitHub
commit 78db623377
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 813 additions and 26 deletions

View file

@ -2904,6 +2904,13 @@ BfCustomAttribute* BfCustomAttributes::Get(BfType* type)
return NULL;
}
BfCustomAttribute* BfCustomAttributes::Get(int idx)
{
if (idx >= mAttributes.size())
return NULL;
return &mAttributes[idx];
}
//////////////////////////////////////////////////////////////////////////
BfResolvedTypeSet::~BfResolvedTypeSet()

View file

@ -2550,6 +2550,7 @@ public:
bool Contains(BfTypeDef* typeDef);
BfCustomAttribute* Get(BfTypeDef* typeDef);
BfCustomAttribute* Get(BfType* type);
BfCustomAttribute* Get(int idx);
void ReportMemory(MemReporter* memReporter);
};

View file

@ -3472,16 +3472,12 @@ bool CeContext::GetStringFromStringView(addr_ce addr, StringImpl& str)
return true;
}
bool CeContext::GetCustomAttribute(BfModule* module, BfIRConstHolder* constHolder, BfCustomAttributes* customAttributes, int attributeTypeId, addr_ce resultAddr)
bool CeContext::GetCustomAttribute(BfModule* module, BfIRConstHolder* constHolder, BfCustomAttributes* customAttributes, int attributeIdx, addr_ce resultAddr)
{
if (customAttributes == NULL)
return false;
BfType* attributeType = GetBfType(attributeTypeId);
if (attributeType == NULL)
return false;
auto customAttr = customAttributes->Get(attributeType);
auto customAttr = customAttributes->Get(attributeIdx);
if (customAttr == NULL)
return false;
@ -3504,6 +3500,18 @@ bool CeContext::GetCustomAttribute(BfModule* module, BfIRConstHolder* constHolde
return true;
}
BfType* CeContext::GetCustomAttributeType(BfCustomAttributes* customAttributes, int attributeIdx)
{
if (customAttributes == NULL)
return NULL;
auto customAttr = customAttributes->Get(attributeIdx);
if (customAttr == NULL)
return NULL;
return customAttr->mType;
}
//#define CE_GETC(T) *((T*)(addr += sizeof(T)) - 1)
#define CE_GETC(T) *(T*)(mMemory.mVals + addr)
@ -5130,7 +5138,7 @@ bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8*
else if (checkFunction->mFunctionKind == CeFunctionKind_Type_GetCustomAttribute)
{
int32 typeId = *(int32*)((uint8*)stackPtr + 1);
int32 attributeTypeId = *(int32*)((uint8*)stackPtr + 1 + 4);
int32 attributeIdx = *(int32*)((uint8*)stackPtr + 1 + 4);
addr_ce resultPtr = *(addr_ce*)((uint8*)stackPtr + 1 + 4 + 4);
BfType* type = GetBfType(typeId);
@ -5139,7 +5147,7 @@ bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8*
{
auto typeInst = type->ToTypeInstance();
if (typeInst != NULL)
success = GetCustomAttribute(mCurModule, typeInst->mConstHolder, typeInst->mCustomAttributes, attributeTypeId, resultPtr);
success = GetCustomAttribute(mCurModule, typeInst->mConstHolder, typeInst->mCustomAttributes, attributeIdx, resultPtr);
_FixVariables();
}
@ -5149,7 +5157,7 @@ bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8*
{
int32 typeId = *(int32*)((uint8*)stackPtr + 1);
int32 fieldIdx = *(int32*)((uint8*)stackPtr + 1 + 4);
int32 attributeTypeId = *(int32*)((uint8*)stackPtr + 1 + 4 + 4);
int32 attributeIdx = *(int32*)((uint8*)stackPtr + 1 + 4 + 4);
addr_ce resultPtr = *(addr_ce*)((uint8*)stackPtr + 1 + 4 + 4 + 4);
BfType* type = GetBfType(typeId);
@ -5164,7 +5172,7 @@ bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8*
if ((fieldIdx >= 0) && (fieldIdx < typeInst->mFieldInstances.mSize))
{
auto& fieldInstance = typeInst->mFieldInstances[fieldIdx];
success = GetCustomAttribute(mCurModule, typeInst->mConstHolder, fieldInstance.mCustomAttributes, attributeTypeId, resultPtr);
success = GetCustomAttribute(mCurModule, typeInst->mConstHolder, fieldInstance.mCustomAttributes, attributeIdx, resultPtr);
_FixVariables();
}
else if (fieldIdx != -1)
@ -5180,7 +5188,7 @@ bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8*
else if (checkFunction->mFunctionKind == CeFunctionKind_Method_GetCustomAttribute)
{
int64 methodHandle = *(int64*)((uint8*)stackPtr + 1);
int32 attributeTypeId = *(int32*)((uint8*)stackPtr + 1 + 8);
int32 attributeIdx = *(int32*)((uint8*)stackPtr + 1 + 8);
addr_ce resultPtr = *(addr_ce*)((uint8*)stackPtr + 1 + 8 + 4);
auto methodInstance = mCeMachine->GetMethodInstance(methodHandle);
@ -5189,10 +5197,82 @@ bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8*
_Fail("Invalid method instance");
return false;
}
bool success = GetCustomAttribute(mCurModule, methodInstance->GetOwner()->mConstHolder, methodInstance->GetCustomAttributes(), attributeTypeId, resultPtr);
bool success = GetCustomAttribute(mCurModule, methodInstance->GetOwner()->mConstHolder, methodInstance->GetCustomAttributes(), attributeIdx, resultPtr);
_FixVariables();
*(addr_ce*)(stackPtr + 0) = success;
}
else if (checkFunction->mFunctionKind == CeFunctionKind_Type_GetCustomAttributeType)
{
int32 typeId = *(int32*)((uint8*)stackPtr + ptrSize);
int32 attributeIdx = *(int32*)((uint8*)stackPtr + ptrSize + 4);
BfType* type = GetBfType(typeId);
addr_ce reflectType = 0;
if (type != NULL)
{
auto typeInst = type->ToTypeInstance();
if (typeInst != NULL)
{
auto attrType = GetCustomAttributeType(typeInst->mCustomAttributes, attributeIdx);
if (attrType != NULL)
reflectType = GetReflectType(attrType->mTypeId);
}
_FixVariables();
}
CeSetAddrVal(stackPtr + 0, reflectType, ptrSize);
}
else if (checkFunction->mFunctionKind == CeFunctionKind_Field_GetCustomAttributeType)
{
int32 typeId = *(int32*)((uint8*)stackPtr + ptrSize);
int32 fieldIdx = *(int32*)((uint8*)stackPtr + ptrSize + 4);
int32 attributeIdx = *(int32*)((uint8*)stackPtr + ptrSize + 4 + 4);
BfType* type = GetBfType(typeId);
addr_ce reflectType = 0;
if (type != NULL)
{
auto typeInst = type->ToTypeInstance();
if (typeInst != NULL)
{
if (typeInst->mDefineState < BfTypeDefineState_CETypeInit)
mCurModule->PopulateType(typeInst);
if ((fieldIdx >= 0) && (fieldIdx < typeInst->mFieldInstances.mSize))
{
auto& fieldInstance = typeInst->mFieldInstances[fieldIdx];
auto attrType = GetCustomAttributeType(fieldInstance.mCustomAttributes, attributeIdx);
if (attrType != NULL)
reflectType = GetReflectType(attrType->mTypeId);
_FixVariables();
}
else if (fieldIdx != -1)
{
_Fail("Invalid field");
return false;
}
}
}
CeSetAddrVal(stackPtr + 0, reflectType, ptrSize);
}
else if (checkFunction->mFunctionKind == CeFunctionKind_Method_GetCustomAttributeType)
{
int64 methodHandle = *(int64*)((uint8*)stackPtr + ptrSize);
int32 attributeIdx = *(int32*)((uint8*)stackPtr + ptrSize + 8);
auto methodInstance = mCeMachine->GetMethodInstance(methodHandle);
if (methodInstance == NULL)
{
_Fail("Invalid method instance");
return false;
}
auto attrType = GetCustomAttributeType(methodInstance->GetCustomAttributes(), attributeIdx);
if (attrType != NULL)
CeSetAddrVal(stackPtr + 0, GetReflectType(attrType->mTypeId), ptrSize);
else
CeSetAddrVal(stackPtr + 0, 0, ptrSize);
_FixVariables();
}
else if (checkFunction->mFunctionKind == CeFunctionKind_GetMethodCount)
{
int32 typeId = *(int32*)((uint8*)stackPtr + 4);
@ -8152,6 +8232,18 @@ void CeMachine::CheckFunctionKind(CeFunction* ceFunction)
{
ceFunction->mFunctionKind = CeFunctionKind_Method_GetCustomAttribute;
}
else if (methodDef->mName == "Comptime_Type_GetCustomAttributeType")
{
ceFunction->mFunctionKind = CeFunctionKind_Type_GetCustomAttributeType;
}
else if (methodDef->mName == "Comptime_Field_GetCustomAttributeType")
{
ceFunction->mFunctionKind = CeFunctionKind_Field_GetCustomAttributeType;
}
else if (methodDef->mName == "Comptime_Method_GetCustomAttributeType")
{
ceFunction->mFunctionKind = CeFunctionKind_Method_GetCustomAttributeType;
}
else if (methodDef->mName == "Comptime_GetMethod")
{
ceFunction->mFunctionKind = CeFunctionKind_GetMethod;

View file

@ -328,6 +328,9 @@ enum CeFunctionKind
CeFunctionKind_Type_GetCustomAttribute,
CeFunctionKind_Field_GetCustomAttribute,
CeFunctionKind_Method_GetCustomAttribute,
CeFunctionKind_Type_GetCustomAttributeType,
CeFunctionKind_Field_GetCustomAttributeType,
CeFunctionKind_Method_GetCustomAttributeType,
CeFunctionKind_GetMethodCount,
CeFunctionKind_GetMethod,
CeFunctionKind_Method_ToString,
@ -908,7 +911,8 @@ public:
bool CheckMemory(addr_ce addr, int32 size);
bool GetStringFromAddr(addr_ce strInstAddr, StringImpl& str);
bool GetStringFromStringView(addr_ce addr, StringImpl& str);
bool GetCustomAttribute(BfModule* module, BfIRConstHolder* constHolder, BfCustomAttributes* customAttributes, int attributeTypeId, addr_ce resultAddr);
bool GetCustomAttribute(BfModule* module, BfIRConstHolder* constHolder, BfCustomAttributes* customAttributes, int attributeIdx, addr_ce resultAddr);
BfType* GetCustomAttributeType(BfCustomAttributes* customAttributes, int attributeIdx);
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);