Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generic method instantiation #46

Open
wants to merge 3 commits into
base: metadata-provider
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions MetadataProvider/AssemblyExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -971,8 +971,10 @@ private IInstruction ExtractInstruction(ILInstruction operation)
break;

case SRM.ILOpCode.Ldftn:
instruction = ProcessLoadMethodAddress(operation, false);
break;
case SRM.ILOpCode.Ldvirtftn:
instruction = ProcessLoadMethodAddress(operation);
instruction = ProcessLoadMethodAddress(operation, true);
break;

case SRM.ILOpCode.Ldc_i4:
Expand Down Expand Up @@ -1305,7 +1307,10 @@ private IMethodReference GetMethodReference(SRM.MethodSpecificationHandle handle
CreateGenericParameterReferences(GenericParameterKind.Method, genericArguments.Length);

var method = GetMethodReference(methodspec.Method);
method = method.Instantiate(genericArguments);
// method might have not been extracted yet so some fields might be missing (ex: returnType, isStatic, etc).
// a proxy is used instead so when the method is actually extracted, this instantiated one is updated as well.
// Otherwise the instantiated one would have some null fields.
method = new MethodInstantiationProxy(method, genericArguments);

BindGenericParameterReferences(GenericParameterKind.Method, method);
return method;
Expand Down Expand Up @@ -1573,10 +1578,24 @@ private IInstruction ProcessLoadField(ILInstruction op)
return instruction;
}

private IInstruction ProcessLoadMethodAddress(ILInstruction op)
private IInstruction ProcessLoadMethodAddress(ILInstruction op, bool isVirtual)
{
var operation = OperationHelper.ToLoadMethodAddressOperation(op.Opcode);
var method = GetOperand<IMethodReference>(op);
switch (method)
{
case MethodDefinition methodDefinition:
{
methodDefinition.IsVirtual = isVirtual;
break;
}
case MethodReference methodReference:
{
methodReference.IsVirtual = isVirtual;
break;
}
default: throw new Exception("case not handled");
}

var instruction = new LoadMethodAddressInstruction(op.Offset, operation, method);
return instruction;
Expand Down
31 changes: 31 additions & 0 deletions Model/Types/TypeDefinitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ public interface IMethodReference : ITypeMemberReference, IMetadataReference, IG
IMethodReference GenericMethod { get; }
MethodDefinition ResolvedMethod { get; }
bool IsStatic { get; }
bool IsVirtual { get; }
}

public class MethodReference : IMethodReference
Expand All @@ -302,6 +303,7 @@ public class MethodReference : IMethodReference
public IList<IMethodParameterReference> Parameters { get; private set; }
public IMethodReference GenericMethod { get; set; }
public bool IsStatic { get; set; }
public bool IsVirtual { get; set; }

public MethodReference(string name, IType returnType)
{
Expand Down Expand Up @@ -585,6 +587,35 @@ public override string ToString()
return result.ToString();
}
}

public class MethodInstantiationProxy : IMethodReference
{
public IBasicType ContainingType => method.ContainingType;
public ISet<CustomAttribute> Attributes => method.Attributes;
public int GenericParameterCount => method.GenericParameterCount;
public IType ReturnType => method.ReturnType;
public string Name => method.Name;
public string GenericName => method.GenericName;
public IList<IMethodParameterReference> Parameters => method.Parameters;
public IList<IType> GenericArguments => genericArguments;
public IMethodReference GenericMethod => method;

public MethodDefinition ResolvedMethod =>
throw new InvalidOperationException("Use Resolve method to bind this reference with some host.");

public bool IsStatic => method.IsStatic;
public bool IsVirtual => method.IsVirtual;

private readonly IMethodReference method;
private readonly IList<IType> genericArguments;

public MethodInstantiationProxy(IMethodReference method, IEnumerable<IType> genericArguments)
{
this.method = method;
this.genericArguments = new List<IType>();
this.genericArguments.AddRange(genericArguments);
}
}

public enum TypeDefinitionKind
{
Expand Down