Skip to content

Commit

Permalink
object instead of Type in Call Analysis Attributes (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
ds5678 authored Jan 1, 2024
1 parent 451ee4b commit 3d6a81f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ namespace Cpp2IL.Core.Model.Contexts;

public class GenericParameterTypeAnalysisContext : ReferencedTypeAnalysisContext
{
public override string DefaultName { get; }
public sealed override string DefaultName { get; }

public sealed override string DefaultNs => "";

public int Index { get; }

Expand Down
13 changes: 13 additions & 0 deletions Cpp2IL.Core/Model/CustomAttributes/CustomAttributeNullParameter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.IO;
using Cpp2IL.Core.Model.Contexts;

namespace Cpp2IL.Core.Model.CustomAttributes;

public sealed class CustomAttributeNullParameter : BaseCustomAttributeParameter
{
public CustomAttributeNullParameter(AnalyzedCustomAttribute owner, CustomAttributeParameterKind kind, int index) : base(owner, kind, index)
{
}

public override void ReadFromV29Blob(BinaryReader reader, ApplicationAnalysisContext context) => throw new System.NotSupportedException();
}
27 changes: 17 additions & 10 deletions Cpp2IL.Core/ProcessingLayers/CallAnalysisProcessingLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,17 @@ private static void AddAttribute((InjectedMethodAnalysisContext, InjectedFieldAn
}
else
{
typeField = (callsAttributeInfo.Item2[1], typeFullName);
typeField = (callsAttributeInfo.Item2[0], typeFullName);
}

var memberField = (callsAttributeInfo.Item2[2], targetMethod.Name);
var memberField = (callsAttributeInfo.Item2[1], targetMethod.Name);

(FieldAnalysisContext, object)? typeParametersField;
if (targetMethod is ConcreteGenericMethodAnalysisContext concreteMethod)
{
if (concreteMethod.MethodRef.MethodGenericParams.Length > 0)
{
var parameters = new TypeAnalysisContext[concreteMethod.MethodRef.MethodGenericParams.Length];
var parameters = new object?[concreteMethod.MethodRef.MethodGenericParams.Length];

for (var i = 0; i < parameters.Length; i++)
{
Expand All @@ -194,9 +194,13 @@ private static void AddAttribute((InjectedMethodAnalysisContext, InjectedFieldAn
{
parameters[i] = parameterType;
}
else
{
parameters[i] = parameterType?.FullName;
}
}

typeParametersField = (callsAttributeInfo.Item2[3], parameters);
typeParametersField = (callsAttributeInfo.Item2[2], parameters);
}
else
{
Expand All @@ -211,7 +215,7 @@ private static void AddAttribute((InjectedMethodAnalysisContext, InjectedFieldAn
(FieldAnalysisContext, object)? parametersField;
if (targetMethod.ParameterCount > 0)
{
var parameters = new TypeAnalysisContext[targetMethod.ParameterCount];
var parameters = new object?[targetMethod.ParameterCount];

for (var i = 0; i < parameters.Length; i++)
{
Expand All @@ -220,9 +224,13 @@ private static void AddAttribute((InjectedMethodAnalysisContext, InjectedFieldAn
{
parameters[i] = parameterType;
}
else
{
parameters[i] = parameterType?.FullName;
}
}

parametersField = (callsAttributeInfo.Item2[4], parameters);
parametersField = (callsAttributeInfo.Item2[3], parameters);
}
else
{
Expand All @@ -245,11 +253,10 @@ private static void AddAttribute((InjectedMethodAnalysisContext, InjectedFieldAn
methodName,
AttributeTargets.Method,
true,
(appContext.SystemTypes.SystemTypeType, "Type"),
(appContext.SystemTypes.SystemStringType, "TypeFullName"),
(appContext.SystemTypes.SystemObjectType, "Type"),
(appContext.SystemTypes.SystemStringType, "Member"),
(appContext.SystemTypes.SystemTypeType.MakeSzArrayType(), "MemberTypeParameters"),
(appContext.SystemTypes.SystemTypeType.MakeSzArrayType(), "MemberParameters"));
(appContext.SystemTypes.SystemObjectType.MakeSzArrayType(), "MemberTypeParameters"),
(appContext.SystemTypes.SystemObjectType.MakeSzArrayType(), "MemberParameters"));
}

private static bool TryGetCommonMethodFromList(List<MethodAnalysisContext> methods, [NotNullWhen(true)] out MethodAnalysisContext? commonMethod)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ private static CustomAttributeArgument BuildArrayArgument(AssemblyDefinition par
CustomAttributePrimitiveParameter primitiveParameter => primitiveParameter.PrimitiveValue,
CustomAttributeEnumParameter enumParameter => enumParameter.UnderlyingPrimitiveParameter.PrimitiveValue,
BaseCustomAttributeTypeParameter type => (object?)type.TypeContext?.ToTypeSignature(parentAssembly.ManifestModule!),
CustomAttributeNullParameter => null,
_ => throw new("Not supported array element type: " + e.GetType().FullName)
};

Expand Down
25 changes: 19 additions & 6 deletions Cpp2IL.Core/Utils/AttributeInjectionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,29 @@ private static Il2CppType GetAttributeTargetsType(AssemblyAnalysisContext mscorl

private static BaseCustomAttributeParameter MakeFieldParameter(object fieldValue, AnalyzedCustomAttribute owner, int index)
{
return fieldValue switch
return MakeCustomAttributeParameter(fieldValue, owner, index, CustomAttributeParameterKind.Field);
}

private static BaseCustomAttributeParameter MakeCustomAttributeParameter(object? value, AnalyzedCustomAttribute owner, int index, CustomAttributeParameterKind parameterKind)
{
return value switch
{
Il2CppType type => new CustomAttributeTypeParameter(type, owner, CustomAttributeParameterKind.Field, index),
TypeAnalysisContext type => new InjectedCustomAttributeTypeParameter(type, owner, CustomAttributeParameterKind.Field, index),
TypeAnalysisContext[] types => new CustomAttributeArrayParameter(owner, CustomAttributeParameterKind.Field, index)
Il2CppType type => new CustomAttributeTypeParameter(type, owner, parameterKind, index),
TypeAnalysisContext type => new InjectedCustomAttributeTypeParameter(type, owner, parameterKind, index),
TypeAnalysisContext?[] types => new CustomAttributeArrayParameter(owner, parameterKind, index)
{
ArrType = Il2CppTypeEnum.IL2CPP_TYPE_IL2CPP_TYPE_INDEX,
ArrayElements = types.Select(t => new InjectedCustomAttributeTypeParameter(t, owner, CustomAttributeParameterKind.ArrayElement, index)).Cast<BaseCustomAttributeParameter>().ToList()
ArrayElements = types.Select(t => (BaseCustomAttributeParameter)new InjectedCustomAttributeTypeParameter(t, owner, CustomAttributeParameterKind.ArrayElement, index)).ToList()
},
object?[] objects => new CustomAttributeArrayParameter(owner, parameterKind, index)
{
ArrType = Il2CppTypeEnum.IL2CPP_TYPE_OBJECT,
ArrayElements = objects.Select(obj => obj is null
? new CustomAttributeNullParameter(owner, CustomAttributeParameterKind.ArrayElement, index)
: MakeCustomAttributeParameter(obj, owner, index, CustomAttributeParameterKind.ArrayElement)).ToList()
},
IConvertible convertible => new CustomAttributePrimitiveParameter(convertible, owner, CustomAttributeParameterKind.Field, index),
IConvertible convertible => new CustomAttributePrimitiveParameter(convertible, owner, parameterKind, index),
null => new CustomAttributeNullParameter(owner, parameterKind, index),
_ => throw new NotSupportedException(),
};
}
Expand Down

0 comments on commit 3d6a81f

Please sign in to comment.