1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 11:38:21 +02:00

Reflection support for method param attributes, return attributes

This commit is contained in:
Brian Fiete 2021-07-06 10:55:38 -07:00
parent 19513d3310
commit c10565678e
4 changed files with 38 additions and 14 deletions

View file

@ -37,11 +37,22 @@ namespace System.Reflection
return mMethodData.mParamData[paramIdx].mName; return mMethodData.mParamData[paramIdx].mName;
} }
public Result<T> GetParamCustomAttribute<T>(int paramIdx) where T : Attribute
{
Debug.Assert((uint)paramIdx < (uint)mMethodData.mParamCount);
return mTypeInstance.[Friend]GetCustomAttribute<T>(mMethodData.mParamData[paramIdx].mCustomAttributesIdx);
}
public Result<T> GetCustomAttribute<T>() where T : Attribute public Result<T> GetCustomAttribute<T>() where T : Attribute
{ {
return mTypeInstance.[Friend]GetCustomAttribute<T>(mMethodData.mCustomAttributesIdx); return mTypeInstance.[Friend]GetCustomAttribute<T>(mMethodData.mCustomAttributesIdx);
} }
public Result<T> GetReturnCustomAttribute<T>() where T : Attribute
{
return mTypeInstance.[Friend]GetCustomAttribute<T>(mMethodData.mReturnCustomAttributesIdx);
}
public enum CallError public enum CallError
{ {
case None; case None;

View file

@ -718,6 +718,7 @@ namespace System.Reflection
public int32 mMethodIdx; public int32 mMethodIdx;
public int32 mVirtualIdx; public int32 mVirtualIdx;
public int32 mCustomAttributesIdx; public int32 mCustomAttributesIdx;
public int32 mReturnCustomAttributesIdx;
} }
public enum ParamFlags : int16 public enum ParamFlags : int16
@ -734,6 +735,7 @@ namespace System.Reflection
public TypeId mType; public TypeId mType;
public ParamFlags mParamFlags; public ParamFlags mParamFlags;
public int32 mDefaultIdx; public int32 mDefaultIdx;
public int32 mCustomAttributesIdx;
} }
public struct InterfaceData public struct InterfaceData

View file

@ -679,6 +679,7 @@ namespace System.Reflection
public int32 mMethodIdx; public int32 mMethodIdx;
public int32 mVirtualIdx; public int32 mVirtualIdx;
public int32 mCustomAttributesIdx; public int32 mCustomAttributesIdx;
public int32 mReturnCustomAttributesIdx;
} }
public enum ParamFlags : int16 public enum ParamFlags : int16
@ -695,6 +696,7 @@ namespace System.Reflection
public TypeId mType; public TypeId mType;
public ParamFlags mParamFlags; public ParamFlags mParamFlags;
public int32 mDefaultIdx; public int32 mDefaultIdx;
public int32 mCustomAttributesIdx;
} }
public struct InterfaceData public struct InterfaceData

View file

@ -6501,7 +6501,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
struct _SortedMethodInfo struct _SortedMethodInfo
{ {
BfMethodDef* mMethodDef; BfMethodDef* mMethodDef;
BfCustomAttributes* mMethodCustomAttributes; BfMethodCustomAttributes* mMethodCustomAttributes;
}; };
Array<_SortedMethodInfo> sortedMethodList; Array<_SortedMethodInfo> sortedMethodList;
@ -6542,10 +6542,10 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
bool includeMethod = reflectIncludeAllMethods; bool includeMethod = reflectIncludeAllMethods;
BfCustomAttributes* methodCustomAttributes = NULL; BfMethodCustomAttributes* methodCustomAttributes = NULL;
if ((defaultMethod->mMethodInfoEx != NULL) && (defaultMethod->mMethodInfoEx->mMethodCustomAttributes != NULL) && (defaultMethod->mMethodInfoEx->mMethodCustomAttributes->mCustomAttributes != NULL)) if ((defaultMethod->mMethodInfoEx != NULL) && (defaultMethod->mMethodInfoEx->mMethodCustomAttributes != NULL) && (defaultMethod->mMethodInfoEx->mMethodCustomAttributes->mCustomAttributes != NULL))
{ {
methodCustomAttributes = defaultMethod->mMethodInfoEx->mMethodCustomAttributes->mCustomAttributes; methodCustomAttributes = defaultMethod->mMethodInfoEx->mMethodCustomAttributes;
for (auto& customAttr : defaultMethod->mMethodInfoEx->mMethodCustomAttributes->mCustomAttributes->mAttributes) for (auto& customAttr : defaultMethod->mMethodInfoEx->mMethodCustomAttributes->mCustomAttributes->mAttributes)
{ {
if (customAttr.mType->mTypeDef->mName->ToString() == "ReflectAttribute") if (customAttr.mType->mTypeDef->mName->ToString() == "ReflectAttribute")
@ -6643,7 +6643,13 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
BfMethodFlags methodFlags = defaultMethod->GetMethodFlags(); BfMethodFlags methodFlags = defaultMethod->GetMethodFlags();
int customAttrIdx = _HandleCustomAttrs(methodInfo.mMethodCustomAttributes); int customAttrIdx = -1;
int returnCustomAttrIdx = -1;
if (methodInfo.mMethodCustomAttributes != NULL)
{
customAttrIdx = _HandleCustomAttrs(methodInfo.mMethodCustomAttributes->mCustomAttributes);
returnCustomAttrIdx = _HandleCustomAttrs(methodInfo.mMethodCustomAttributes->mReturnCustomAttributes);
}
enum ParamFlags enum ParamFlags
{ {
@ -6664,13 +6670,18 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
BfIRValue paramNameConst = GetStringObjectValue(paramName, !mIsComptimeModule); BfIRValue paramNameConst = GetStringObjectValue(paramName, !mIsComptimeModule);
int paramCustomAttrIdx = -1;
if ((methodInfo.mMethodCustomAttributes != NULL) && (paramIdx < (int)methodInfo.mMethodCustomAttributes->mParamCustomAttributes.mSize))
paramCustomAttrIdx = _HandleCustomAttrs(methodInfo.mMethodCustomAttributes->mParamCustomAttributes[paramIdx]);
SizedArray<BfIRValue, 8> paramDataVals = SizedArray<BfIRValue, 8> paramDataVals =
{ {
emptyValueType, emptyValueType,
paramNameConst, paramNameConst,
GetConstValue(paramType->mTypeId, typeIdType), GetConstValue(paramType->mTypeId, typeIdType),
GetConstValue((int32)paramFlags, shortType), GetConstValue((int32)paramFlags, shortType),
GetConstValue(customAttrIdx, intType) // defaultIdx GetConstValue(-1, intType), // defaultIdx
GetConstValue(paramCustomAttrIdx, intType) // customAttrIdx
}; };
auto paramData = mBfIRBuilder->CreateConstAgg_Value(mBfIRBuilder->MapType(reflectParamDataType, BfIRPopulateType_Full), paramDataVals); auto paramData = mBfIRBuilder->CreateConstAgg_Value(mBfIRBuilder->MapType(reflectParamDataType, BfIRPopulateType_Full), paramDataVals);
paramVals.Add(paramData); paramVals.Add(paramData);
@ -6739,6 +6750,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
GetConstValue(methodIdx, intType), GetConstValue(methodIdx, intType),
GetConstValue(vDataVal, intType), GetConstValue(vDataVal, intType),
GetConstValue(customAttrIdx, intType), GetConstValue(customAttrIdx, intType),
GetConstValue(returnCustomAttrIdx, intType),
}; };
auto methodData = mBfIRBuilder->CreateConstAgg_Value(mBfIRBuilder->MapTypeInst(reflectMethodDataType->ToTypeInstance(), BfIRPopulateType_Full), methodDataVals); auto methodData = mBfIRBuilder->CreateConstAgg_Value(mBfIRBuilder->MapTypeInst(reflectMethodDataType->ToTypeInstance(), BfIRPopulateType_Full), methodDataVals);
methodTypes.push_back(methodData); methodTypes.push_back(methodData);
@ -11369,14 +11381,13 @@ void BfModule::GetCustomAttributes(BfCustomAttributes* customAttributes, BfAttri
{ {
Fail(StrFormat("'%s' is not a valid attribute location for this declaration. Valid attribute locations for this declaration are '%s'. All attributes in this block will be ignored.", Fail(StrFormat("'%s' is not a valid attribute location for this declaration. Valid attribute locations for this declaration are '%s'. All attributes in this block will be ignored.",
GetAttributesTargetListString(targetOverride).c_str(), GetAttributesTargetListString(attrTarget).c_str()), attributesDirective->mAttributeTargetSpecifier); // CS0657 GetAttributesTargetListString(targetOverride).c_str(), GetAttributesTargetListString(attrTarget).c_str()), attributesDirective->mAttributeTargetSpecifier); // CS0657
} success = false;
}
success = false;
} }
if ((success) && (targetOverride != (BfAttributeTargets)0)) if ((success) && (targetOverride != (BfAttributeTargets)0))
{ {
if ((targetOverride == BfAttributeTargets_ReturnValue) && (attrTarget == BfAttributeTargets_Method)) if ((mCurMethodInstance != NULL) && (targetOverride == BfAttributeTargets_ReturnValue) && (attrTarget == BfAttributeTargets_Method))
{ {
auto methodInfoEx = mCurMethodInstance->GetMethodInfoEx(); auto methodInfoEx = mCurMethodInstance->GetMethodInfoEx();
@ -11389,11 +11400,9 @@ void BfModule::GetCustomAttributes(BfCustomAttributes* customAttributes, BfAttri
methodInfoEx->mMethodCustomAttributes->mReturnCustomAttributes->mAttributes.push_back(customAttribute); methodInfoEx->mMethodCustomAttributes->mReturnCustomAttributes->mAttributes.push_back(customAttribute);
} }
} }
else
{ // Mark as failed since we don't actually want to add this to the custom attributes set
// Failed - ignore success = false;
success = false;
}
} }
if (success) if (success)