Skip to content

Commit

Permalink
Generic fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Byass committed Sep 23, 2021
1 parent 07808f2 commit 5278fc8
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public Arm64MetadataUsageMethodRefToRegisterAction(MethodAnalysis<Arm64Instructi
if (genericTypeParams.Count > 0)
{
declaringType = declaringType.MakeGenericInstanceType(genericTypeParams.ToArray());
method = method.MakeGeneric(genericTypeParams.ToArray());
method = method.MakeMethodOnGenericType(genericTypeParams.ToArray());
}

if (genericMethodParams.Count > 0)
Expand Down
3 changes: 3 additions & 0 deletions Cpp2IL.Core/Analysis/Actions/Base/AbstractCallAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ public override Mono.Cecil.Cil.Instruction[] ToILInstructions(MethodAnalysis<T>
if(toCall is GenericInstanceMethod gim2)
toCall = processor.ImportRecursive(gim2);

// if (toCall.DeclaringType is GenericInstanceType git2)
// toCall.DeclaringType = processor.ImportRecursive(git2);

result.Add(processor.Create(ShouldUseCallvirt ? OpCodes.Callvirt : OpCodes.Call, processor.ImportReference(toCall)));

if (ManagedMethodBeingCalled.ReturnType.FullName == "System.Void")
Expand Down
2 changes: 1 addition & 1 deletion Cpp2IL.Core/Analysis/Actions/Base/AbstractNewObjAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override Instruction[] ToILInstructions(MethodAnalysis<T> context, ILProc
ctorToCall = TypeCreated?.Resolve()?.Methods.FirstOrDefault(m => m.Name == ".ctor" && m.Parameters.Count == ctorToCall.Parameters.Count) ?? throw new TaintedInstructionException($"Could not resolve a constructor with {ctorToCall.Parameters.Count} parameters.");

if (ctorToCall.HasGenericParameters && TypeCreated is GenericInstanceType git)
ctorToCall = ctorToCall.MakeGeneric(git.GenericArguments.ToArray());
ctorToCall = ctorToCall.MakeMethodOnGenericType(git.GenericArguments.ToArray());

result.Add(processor.Create(OpCodes.Newobj, processor.ImportReference(ctorToCall)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public CallMethodSpecAction(MethodAnalysis<Instruction> context, Instruction ins
ShouldUseCallvirt = true;

if (methodSpec.classIndexIndex != -1)
ManagedMethodBeingCalled = ManagedMethodBeingCalled.MakeGeneric(methodSpec.GenericClassParams.Select(p => Utils.TryResolveTypeReflectionData(p, ManagedMethodBeingCalled)).ToArray()!);
ManagedMethodBeingCalled = ManagedMethodBeingCalled.MakeMethodOnGenericType(methodSpec.GenericClassParams.Select(p => Utils.TryResolveTypeReflectionData(p, ManagedMethodBeingCalled)).ToArray()!);

CreateLocalForReturnType(context);
RegisterLocals();
Expand Down
2 changes: 1 addition & 1 deletion Cpp2IL.Core/AssemblyPopulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private static void FixupExplicitOverridesInType(TypeDefinition ilTypeDefinition
{
//Non-null assertion because we've null-checked the params above.
genericParams = genericParams.Select(p => p is GenericParameter ? p : ilTypeDefinition.Module.ImportReference(p, currentlyFixingUp)).ToList()!;
baseRef = nonGenericRef.MakeGeneric(genericParams.ToArray()!);
baseRef = nonGenericRef.MakeMethodOnGenericType(genericParams.ToArray()!);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions Cpp2IL.Core/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,11 @@ private static TypeReference MakeGenericType(this TypeReference self, params Typ
return instance;
}

public static MethodReference MakeGeneric(this MethodReference self, params TypeReference[] arguments)
public static MethodReference MakeMethodOnGenericType(this MethodReference self, params TypeReference[] typeParams)
{
var reference = new MethodReference(self.Name, self.ReturnType)
{
DeclaringType = self.DeclaringType.MakeGenericType(arguments),
DeclaringType = self.DeclaringType.MakeGenericType(typeParams),
HasThis = self.HasThis,
ExplicitThis = self.ExplicitThis,
CallingConvention = self.CallingConvention
Expand Down

0 comments on commit 5278fc8

Please sign in to comment.