Skip to content
Merged
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
4 changes: 0 additions & 4 deletions Mono.Cecil/AssemblyReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2326,10 +2326,6 @@ MemberReference ReadMemberReference (uint rid)
}

member.token = new MetadataToken (TokenType.MemberRef, rid);

if (module.IsWindowsMetadata ())
WindowsRuntimeProjections.Project (member);

return member;
}

Expand Down
8 changes: 4 additions & 4 deletions Mono.Cecil/AssemblyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,8 @@ void AttachTokens ()

void AttachTypeToken (TypeDefinition type)
{
var treatment = WindowsRuntimeProjections.RemoveProjection (type);

type.token = new MetadataToken (TokenType.TypeDef, type_rid++);
type.fields_range.Start = field_rid;
type.methods_range.Start = method_rid;
Expand All @@ -1289,6 +1291,8 @@ void AttachTypeToken (TypeDefinition type)

if (type.HasNestedTypes)
AttachNestedTypesToken (type);

WindowsRuntimeProjections.ApplyProjection (type, treatment);
}

void AttachNestedTypesToken (TypeDefinition type)
Expand Down Expand Up @@ -1990,16 +1994,12 @@ void AddSecurityDeclarations (ISecurityDeclarationProvider owner)

MetadataToken GetMemberRefToken (MemberReference member)
{
var projection = WindowsRuntimeProjections.RemoveProjection (member);

var row = CreateMemberRefRow (member);

MetadataToken token;
if (!member_ref_map.TryGetValue (row, out token))
token = AddMemberReference (member, row);

WindowsRuntimeProjections.ApplyProjection (member, projection);

return token;
}

Expand Down
2 changes: 1 addition & 1 deletion Mono.Cecil/FieldDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public int Offset {
set { offset = value; }
}

internal new FieldDefinitionProjection WindowsRuntimeProjection {
internal FieldDefinitionProjection WindowsRuntimeProjection {
get { return (FieldDefinitionProjection) projection; }
set { projection = value; }
}
Expand Down
175 changes: 175 additions & 0 deletions Mono.Cecil/GenericParameterResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
using Mono.Cecil.Cil;
using System;

namespace Mono.Cecil {
internal sealed class GenericParameterResolver {
internal static TypeReference ResolveReturnTypeIfNeeded (MethodReference methodReference)
{
if (methodReference.DeclaringType.IsArray && methodReference.Name == "Get")
return methodReference.ReturnType;

var genericInstanceMethod = methodReference as GenericInstanceMethod;
var declaringGenericInstanceType = methodReference.DeclaringType as GenericInstanceType;

if (genericInstanceMethod == null && declaringGenericInstanceType == null)
return methodReference.ReturnType;

return ResolveIfNeeded (genericInstanceMethod, declaringGenericInstanceType, methodReference.ReturnType);
}

internal static TypeReference ResolveFieldTypeIfNeeded (FieldReference fieldReference)
{
return ResolveIfNeeded (null, fieldReference.DeclaringType as GenericInstanceType, fieldReference.FieldType);
}

internal static TypeReference ResolveParameterTypeIfNeeded (MethodReference method, ParameterReference parameter)
{
var genericInstanceMethod = method as GenericInstanceMethod;
var declaringGenericInstanceType = method.DeclaringType as GenericInstanceType;

if (genericInstanceMethod == null && declaringGenericInstanceType == null)
return parameter.ParameterType;

return ResolveIfNeeded (genericInstanceMethod, declaringGenericInstanceType, parameter.ParameterType);
}

internal static TypeReference ResolveVariableTypeIfNeeded (MethodReference method, VariableReference variable)
{
var genericInstanceMethod = method as GenericInstanceMethod;
var declaringGenericInstanceType = method.DeclaringType as GenericInstanceType;

if (genericInstanceMethod == null && declaringGenericInstanceType == null)
return variable.VariableType;

return ResolveIfNeeded (genericInstanceMethod, declaringGenericInstanceType, variable.VariableType);
}

private static TypeReference ResolveIfNeeded (IGenericInstance genericInstanceMethod, IGenericInstance declaringGenericInstanceType, TypeReference parameterType)
{
var byRefType = parameterType as ByReferenceType;
if (byRefType != null)
return ResolveIfNeeded (genericInstanceMethod, declaringGenericInstanceType, byRefType);

var arrayType = parameterType as ArrayType;
if (arrayType != null)
return ResolveIfNeeded (genericInstanceMethod, declaringGenericInstanceType, arrayType);

var genericInstanceType = parameterType as GenericInstanceType;
if (genericInstanceType != null)
return ResolveIfNeeded (genericInstanceMethod, declaringGenericInstanceType, genericInstanceType);

var genericParameter = parameterType as GenericParameter;
if (genericParameter != null)
return ResolveIfNeeded (genericInstanceMethod, declaringGenericInstanceType, genericParameter);

var requiredModifierType = parameterType as RequiredModifierType;
if (requiredModifierType != null && ContainsGenericParameters (requiredModifierType))
return ResolveIfNeeded (genericInstanceMethod, declaringGenericInstanceType, requiredModifierType.ElementType);

if (ContainsGenericParameters (parameterType))
throw new Exception ("Unexpected generic parameter.");

return parameterType;
}

private static TypeReference ResolveIfNeeded (IGenericInstance genericInstanceMethod, IGenericInstance genericInstanceType, GenericParameter genericParameterElement)
{
return (genericParameterElement.MetadataType == MetadataType.MVar)
? (genericInstanceMethod != null ? genericInstanceMethod.GenericArguments[genericParameterElement.Position] : genericParameterElement)
: genericInstanceType.GenericArguments[genericParameterElement.Position];
}

private static ArrayType ResolveIfNeeded (IGenericInstance genericInstanceMethod, IGenericInstance genericInstanceType, ArrayType arrayType)
{
return new ArrayType (ResolveIfNeeded (genericInstanceMethod, genericInstanceType, arrayType.ElementType), arrayType.Rank);
}

private static ByReferenceType ResolveIfNeeded (IGenericInstance genericInstanceMethod, IGenericInstance genericInstanceType, ByReferenceType byReferenceType)
{
return new ByReferenceType (ResolveIfNeeded (genericInstanceMethod, genericInstanceType, byReferenceType.ElementType));
}

private static GenericInstanceType ResolveIfNeeded (IGenericInstance genericInstanceMethod, IGenericInstance genericInstanceType, GenericInstanceType genericInstanceType1)
{
if (!ContainsGenericParameters (genericInstanceType1))
return genericInstanceType1;

var newGenericInstance = new GenericInstanceType (genericInstanceType1.ElementType);

foreach (var genericArgument in genericInstanceType1.GenericArguments) {
if (!genericArgument.IsGenericParameter) {
newGenericInstance.GenericArguments.Add (ResolveIfNeeded (genericInstanceMethod, genericInstanceType, genericArgument));
continue;
}

var genParam = (GenericParameter)genericArgument;

switch (genParam.Type) {
case GenericParameterType.Type: {
if (genericInstanceType == null)
throw new NotSupportedException ();

newGenericInstance.GenericArguments.Add (genericInstanceType.GenericArguments[genParam.Position]);
}
break;

case GenericParameterType.Method: {
if (genericInstanceMethod == null)
newGenericInstance.GenericArguments.Add (genParam);
else
newGenericInstance.GenericArguments.Add (genericInstanceMethod.GenericArguments[genParam.Position]);
}
break;
}
}

return newGenericInstance;
}

private static bool ContainsGenericParameters (TypeReference typeReference)
{
var genericParameter = typeReference as GenericParameter;
if (genericParameter != null)
return true;

var arrayType = typeReference as ArrayType;
if (arrayType != null)
return ContainsGenericParameters (arrayType.ElementType);

var pointerType = typeReference as PointerType;
if (pointerType != null)
return ContainsGenericParameters (pointerType.ElementType);

var byRefType = typeReference as ByReferenceType;
if (byRefType != null)
return ContainsGenericParameters (byRefType.ElementType);

var sentinelType = typeReference as SentinelType;
if (sentinelType != null)
return ContainsGenericParameters (sentinelType.ElementType);

var pinnedType = typeReference as PinnedType;
if (pinnedType != null)
return ContainsGenericParameters (pinnedType.ElementType);

var requiredModifierType = typeReference as RequiredModifierType;
if (requiredModifierType != null)
return ContainsGenericParameters (requiredModifierType.ElementType);

var genericInstance = typeReference as GenericInstanceType;
if (genericInstance != null) {
foreach (var genericArgument in genericInstance.GenericArguments) {
if (ContainsGenericParameters (genericArgument))
return true;
}

return false;
}

if (typeReference is TypeSpecification)
throw new NotSupportedException ();

return false;
}
}
}
5 changes: 0 additions & 5 deletions Mono.Cecil/MemberReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ public bool IsWindowsRuntimeProjection {
get { return projection != null; }
}

internal MemberReferenceProjection WindowsRuntimeProjection {
get { return (MemberReferenceProjection) projection; }
set { projection = value; }
}

internal bool HasImage {
get {
var module = Module;
Expand Down
2 changes: 1 addition & 1 deletion Mono.Cecil/MethodDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public MethodSemanticsAttributes SemanticsAttributes {
set { sem_attrs = value; }
}

internal new MethodDefinitionProjection WindowsRuntimeProjection {
internal MethodDefinitionProjection WindowsRuntimeProjection {
get { return (MethodDefinitionProjection) projection; }
set { projection = value; }
}
Expand Down
143 changes: 143 additions & 0 deletions Mono.Cecil/MethodReferenceComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
using System;
using System.Collections.Generic;

namespace Mono.Cecil {
internal sealed class MethodReferenceComparer : EqualityComparer<MethodReference> {
// Initialized lazily for each thread
[ThreadStatic]
static List<MethodReference> xComparisonStack = null;

[ThreadStatic]
static List<MethodReference> yComparisonStack = null;

public override bool Equals (MethodReference x, MethodReference y)
{
return AreEqual (x, y);
}

public override int GetHashCode (MethodReference obj)
{
return GetHashCodeFor (obj);
}

public static bool AreEqual (MethodReference x, MethodReference y)
{
if (ReferenceEquals (x, y))
return true;

if (x.HasThis != y.HasThis)
return false;

if (x.HasParameters != y.HasParameters)
return false;

if (x.HasGenericParameters != y.HasGenericParameters)
return false;

if (x.Parameters.Count != y.Parameters.Count)
return false;

if (x.Name != y.Name)
return false;

if (!TypeReferenceEqualityComparer.AreEqual (x.DeclaringType, y.DeclaringType))
return false;

var xGeneric = x as GenericInstanceMethod;
var yGeneric = y as GenericInstanceMethod;
if (xGeneric != null || yGeneric != null) {
if (xGeneric == null || yGeneric == null)
return false;

if (xGeneric.GenericArguments.Count != yGeneric.GenericArguments.Count)
return false;

for (int i = 0; i < xGeneric.GenericArguments.Count; i++)
if (!TypeReferenceEqualityComparer.AreEqual (xGeneric.GenericArguments[i], yGeneric.GenericArguments[i]))
return false;
}

var xResolved = x.Resolve ();
var yResolved = y.Resolve ();

if (xResolved != yResolved)
return false;

if (xResolved == null)
{
// We couldn't resolve either method. In order for them to be equal, their parameter types _must_ match. But wait, there's a twist!
// There exists a situation where we might get into a recursive state: parameter type comparison might lead to comparing the same
// methods again if the parameter types are generic parameters whose owners are these methods. We guard against these by using a
// thread static list of all our comparisons carried out in the stack so far, and if we're in progress of comparing them already,
// we'll just say that they match.

if (xComparisonStack == null)
xComparisonStack = new List<MethodReference> ();

if (yComparisonStack == null)
yComparisonStack = new List<MethodReference> ();

for (int i = 0; i < xComparisonStack.Count; i++) {
if (xComparisonStack[i] == x && yComparisonStack[i] == y)
return true;
}

xComparisonStack.Add (x);

try {
yComparisonStack.Add (y);

try {
for (int i = 0; i < x.Parameters.Count; i++) {
if (!TypeReferenceEqualityComparer.AreEqual (x.Parameters[i].ParameterType, y.Parameters[i].ParameterType))
return false;
}
} finally {
yComparisonStack.RemoveAt (yComparisonStack.Count - 1);
}
} finally {
xComparisonStack.RemoveAt (xComparisonStack.Count - 1);
}
}

return true;
}

public static bool AreSignaturesEqual (MethodReference x, MethodReference y, TypeComparisonMode comparisonMode = TypeComparisonMode.Exact)
{
if (x.HasThis != y.HasThis)
return false;

if (x.Parameters.Count != y.Parameters.Count)
return false;

if (x.GenericParameters.Count != y.GenericParameters.Count)
return false;

for (var i = 0; i < x.Parameters.Count; i++)
if (!TypeReferenceEqualityComparer.AreEqual (x.Parameters[i].ParameterType, y.Parameters[i].ParameterType, comparisonMode))
return false;

if (!TypeReferenceEqualityComparer.AreEqual (x.ReturnType, y.ReturnType, comparisonMode))
return false;

return true;
}

public static int GetHashCodeFor (MethodReference obj)
{
// a very good prime number
const int hashCodeMultiplier = 486187739;

var genericInstanceMethod = obj as GenericInstanceMethod;
if (genericInstanceMethod != null) {
var hashCode = GetHashCodeFor (genericInstanceMethod.ElementMethod);
for (var i = 0; i < genericInstanceMethod.GenericArguments.Count; i++)
hashCode = hashCode * hashCodeMultiplier + TypeReferenceEqualityComparer.GetHashCodeFor (genericInstanceMethod.GenericArguments[i]);
return hashCode;
}

return TypeReferenceEqualityComparer.GetHashCodeFor (obj.DeclaringType) * hashCodeMultiplier + obj.Name.GetHashCode ();
}
}
}
Loading