2016-03-22 23:02:25 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using Mono.Cecil;
|
|
|
|
using Mono.Cecil.Rocks;
|
|
|
|
|
|
|
|
namespace Xamarin.Forms.Build.Tasks
|
|
|
|
{
|
2016-12-07 12:57:43 +03:00
|
|
|
class TypeRefComparer : IEqualityComparer<TypeReference>
|
|
|
|
{
|
|
|
|
static string GetAssembly(TypeReference typeRef)
|
|
|
|
{
|
|
|
|
var md = typeRef.Scope as ModuleDefinition;
|
|
|
|
if (md != null)
|
|
|
|
return md.Assembly.FullName;
|
|
|
|
var anr = typeRef.Scope as AssemblyNameReference;
|
|
|
|
if (anr != null)
|
|
|
|
return anr.FullName;
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(typeRef));
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Equals(TypeReference x, TypeReference y)
|
|
|
|
{
|
2016-12-23 10:50:02 +03:00
|
|
|
if (x.FullName != y.FullName)
|
|
|
|
return false;
|
|
|
|
var xasm = GetAssembly(x);
|
|
|
|
if (xasm.StartsWith("System.Runtime", StringComparison.Ordinal) || xasm.StartsWith("mscorlib", StringComparison.Ordinal))
|
|
|
|
xasm = "mscorlib";
|
|
|
|
var yasm = GetAssembly(y);
|
|
|
|
if (yasm.StartsWith("System.Runtime", StringComparison.Ordinal) || yasm.StartsWith("mscorlib", StringComparison.Ordinal))
|
|
|
|
yasm = "mscorlib";
|
|
|
|
return xasm == yasm;
|
2016-12-07 12:57:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public int GetHashCode(TypeReference obj)
|
|
|
|
{
|
|
|
|
return $"{GetAssembly(obj)}//{obj.FullName}".GetHashCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
static TypeRefComparer s_default;
|
|
|
|
public static TypeRefComparer Default => s_default ?? (s_default = new TypeRefComparer());
|
|
|
|
}
|
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
static class TypeReferenceExtensions
|
|
|
|
{
|
|
|
|
public static PropertyDefinition GetProperty(this TypeReference typeRef, Func<PropertyDefinition, bool> predicate,
|
|
|
|
out TypeReference declaringTypeRef)
|
|
|
|
{
|
|
|
|
declaringTypeRef = typeRef;
|
|
|
|
var typeDef = typeRef.Resolve();
|
|
|
|
var properties = typeDef.Properties.Where(predicate);
|
|
|
|
if (properties.Any())
|
|
|
|
return properties.Single();
|
|
|
|
if (typeDef.BaseType == null || typeDef.BaseType.FullName == "System.Object")
|
|
|
|
return null;
|
|
|
|
return typeDef.BaseType.GetProperty(predicate, out declaringTypeRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static EventDefinition GetEvent(this TypeReference typeRef, Func<EventDefinition, bool> predicate)
|
|
|
|
{
|
|
|
|
var typeDef = typeRef.Resolve();
|
|
|
|
var events = typeDef.Events.Where(predicate);
|
|
|
|
if (events.Any())
|
|
|
|
return events.Single();
|
|
|
|
if (typeDef.BaseType == null || typeDef.BaseType.FullName == "System.Object")
|
|
|
|
return null;
|
|
|
|
return typeDef.BaseType.GetEvent(predicate);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static FieldDefinition GetField(this TypeReference typeRef, Func<FieldDefinition, bool> predicate,
|
|
|
|
out TypeReference declaringTypeRef)
|
|
|
|
{
|
|
|
|
declaringTypeRef = typeRef;
|
|
|
|
var typeDef = typeRef.Resolve();
|
|
|
|
var bp = typeDef.Fields.Where
|
|
|
|
(predicate);
|
|
|
|
if (bp.Any())
|
|
|
|
return bp.Single();
|
|
|
|
if (typeDef.BaseType == null || typeDef.BaseType.FullName == "System.Object")
|
|
|
|
return null;
|
|
|
|
var basetype = typeDef.BaseType.ResolveGenericParameters(typeRef);
|
|
|
|
return basetype.GetField(predicate, out declaringTypeRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool ImplementsInterface(this TypeReference typeRef, TypeReference @interface)
|
|
|
|
{
|
|
|
|
var typeDef = typeRef.Resolve();
|
|
|
|
if (typeDef.Interfaces.Any(tr => tr.FullName == @interface.FullName))
|
|
|
|
return true;
|
|
|
|
var baseTypeRef = typeDef.BaseType;
|
|
|
|
if (baseTypeRef != null && baseTypeRef.FullName != "System.Object")
|
|
|
|
return baseTypeRef.ImplementsInterface(@interface);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool ImplementsGenericInterface(this TypeReference typeRef, string @interface,
|
|
|
|
out GenericInstanceType interfaceReference, out IList<TypeReference> genericArguments)
|
|
|
|
{
|
|
|
|
interfaceReference = null;
|
|
|
|
genericArguments = null;
|
|
|
|
var typeDef = typeRef.Resolve();
|
|
|
|
TypeReference iface;
|
2016-12-05 00:08:11 +03:00
|
|
|
if ((iface = typeDef.Interfaces.FirstOrDefault(tr =>
|
|
|
|
tr.FullName.StartsWith(@interface, StringComparison.Ordinal) &&
|
|
|
|
tr.IsGenericInstance && (tr as GenericInstanceType).HasGenericArguments)) != null)
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
|
|
|
interfaceReference = iface as GenericInstanceType;
|
|
|
|
genericArguments = (iface as GenericInstanceType).GenericArguments;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
var baseTypeRef = typeDef.BaseType;
|
|
|
|
if (baseTypeRef != null && baseTypeRef.FullName != "System.Object")
|
|
|
|
return baseTypeRef.ImplementsGenericInterface(@interface, out interfaceReference, out genericArguments);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-07 12:57:43 +03:00
|
|
|
static readonly string[] arrayInterfaces = {
|
|
|
|
"System.ICloneable",
|
|
|
|
"System.Collections.IEnumerable",
|
|
|
|
"System.Collections.IList",
|
|
|
|
"System.Collections.ICollection",
|
|
|
|
"System.Collections.IStructuralComparable",
|
|
|
|
"System.Collections.IStructuralEquatable",
|
|
|
|
};
|
|
|
|
|
|
|
|
static readonly string[] arrayGenericInterfaces = {
|
|
|
|
"System.Collections.Generic.IEnumerable`1",
|
|
|
|
"System.Collections.Generic.IList`1",
|
|
|
|
"System.Collections.Generic.ICollection`1",
|
|
|
|
"System.Collections.Generic.IReadOnlyCollection`1",
|
|
|
|
"System.Collections.Generic.IReadOnlyList`1",
|
|
|
|
};
|
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
public static bool InheritsFromOrImplements(this TypeReference typeRef, TypeReference baseClass)
|
|
|
|
{
|
2016-12-07 12:57:43 +03:00
|
|
|
if (TypeRefComparer.Default.Equals(typeRef, baseClass))
|
2016-12-02 00:14:22 +03:00
|
|
|
return true;
|
|
|
|
|
2016-12-07 12:57:43 +03:00
|
|
|
if (typeRef.IsValueType)
|
|
|
|
return false;
|
2016-12-05 00:08:11 +03:00
|
|
|
|
2016-12-07 12:57:43 +03:00
|
|
|
if (typeRef.IsArray) {
|
|
|
|
var array = (ArrayType)typeRef;
|
2016-03-22 23:02:25 +03:00
|
|
|
var arrayType = typeRef.Resolve();
|
|
|
|
if (arrayInterfaces.Contains(baseClass.FullName))
|
|
|
|
return true;
|
2016-12-07 12:57:43 +03:00
|
|
|
if (array.IsVector && //generic interfaces are not implemented on multidimensional arrays
|
|
|
|
arrayGenericInterfaces.Contains(baseClass.Resolve().FullName) &&
|
|
|
|
baseClass.IsGenericInstance &&
|
|
|
|
TypeRefComparer.Default.Equals((baseClass as GenericInstanceType).GenericArguments[0], arrayType))
|
2016-03-22 23:02:25 +03:00
|
|
|
return true;
|
2016-12-07 12:57:43 +03:00
|
|
|
return baseClass.FullName == "System.Object";
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
2016-12-05 00:08:11 +03:00
|
|
|
|
2016-12-07 12:57:43 +03:00
|
|
|
if (typeRef.FullName == "System.Object")
|
|
|
|
return false;
|
2016-03-22 23:02:25 +03:00
|
|
|
var typeDef = typeRef.Resolve();
|
2016-12-07 12:57:43 +03:00
|
|
|
if (TypeRefComparer.Default.Equals(typeDef, baseClass.Resolve()))
|
2016-03-22 23:02:25 +03:00
|
|
|
return true;
|
2016-12-07 12:57:43 +03:00
|
|
|
if (typeDef.Interfaces.Any(ir => TypeRefComparer.Default.Equals(ir, baseClass)))
|
2016-03-22 23:02:25 +03:00
|
|
|
return true;
|
|
|
|
if (typeDef.BaseType == null)
|
|
|
|
return false;
|
2016-12-07 12:57:43 +03:00
|
|
|
|
|
|
|
typeRef = typeDef.BaseType.ResolveGenericParameters(typeRef);
|
|
|
|
return typeRef.InheritsFromOrImplements(baseClass);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public static CustomAttribute GetCustomAttribute(this TypeReference typeRef, TypeReference attribute)
|
|
|
|
{
|
|
|
|
var typeDef = typeRef.Resolve();
|
|
|
|
//FIXME: avoid string comparison. make sure the attribute TypeRef is the same one
|
|
|
|
var attr = typeDef.CustomAttributes.SingleOrDefault(ca => ca.AttributeType.FullName == attribute.FullName);
|
|
|
|
if (attr != null)
|
|
|
|
return attr;
|
|
|
|
var baseTypeRef = typeDef.BaseType;
|
|
|
|
if (baseTypeRef != null && baseTypeRef.FullName != "System.Object")
|
|
|
|
return baseTypeRef.GetCustomAttribute(attribute);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
[Obsolete]
|
|
|
|
public static MethodDefinition GetMethod(this TypeReference typeRef, Func<MethodDefinition, bool> predicate)
|
|
|
|
{
|
|
|
|
TypeReference declaringTypeReference;
|
|
|
|
return typeRef.GetMethod(predicate, out declaringTypeReference);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Obsolete]
|
|
|
|
public static MethodDefinition GetMethod(this TypeReference typeRef, Func<MethodDefinition, bool> predicate,
|
|
|
|
out TypeReference declaringTypeRef)
|
|
|
|
{
|
|
|
|
declaringTypeRef = typeRef;
|
|
|
|
var typeDef = typeRef.Resolve();
|
|
|
|
var methods = typeDef.Methods.Where(predicate);
|
|
|
|
if (methods.Any())
|
|
|
|
return methods.Single();
|
|
|
|
if (typeDef.BaseType != null && typeDef.BaseType.FullName == "System.Object")
|
|
|
|
return null;
|
|
|
|
if (typeDef.IsInterface)
|
|
|
|
{
|
|
|
|
foreach (var face in typeDef.Interfaces)
|
|
|
|
{
|
|
|
|
var m = face.GetMethod(predicate);
|
|
|
|
if (m != null)
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return typeDef.BaseType.GetMethod(predicate, out declaringTypeRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IEnumerable<Tuple<MethodDefinition, TypeReference>> GetMethods(this TypeReference typeRef,
|
|
|
|
Func<MethodDefinition, bool> predicate, ModuleDefinition module)
|
|
|
|
{
|
|
|
|
return typeRef.GetMethods((md, tr) => predicate(md), module);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IEnumerable<Tuple<MethodDefinition, TypeReference>> GetMethods(this TypeReference typeRef,
|
|
|
|
Func<MethodDefinition, TypeReference, bool> predicate, ModuleDefinition module)
|
|
|
|
{
|
|
|
|
var typeDef = typeRef.Resolve();
|
|
|
|
foreach (var method in typeDef.Methods.Where(md => predicate(md, typeRef)))
|
|
|
|
yield return new Tuple<MethodDefinition, TypeReference>(method, typeRef);
|
|
|
|
if (typeDef.IsInterface)
|
|
|
|
{
|
|
|
|
foreach (var face in typeDef.Interfaces)
|
|
|
|
{
|
|
|
|
if (face.IsGenericInstance && typeRef is GenericInstanceType)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
foreach (var arg in ((GenericInstanceType)typeRef).GenericArguments)
|
|
|
|
((GenericInstanceType)face).GenericArguments[i++] = module.Import(arg);
|
|
|
|
}
|
|
|
|
foreach (var tuple in face.GetMethods(predicate, module))
|
|
|
|
yield return tuple;
|
|
|
|
}
|
|
|
|
yield break;
|
|
|
|
}
|
|
|
|
if (typeDef.BaseType == null || typeDef.BaseType.FullName == "System.Object")
|
|
|
|
yield break;
|
|
|
|
var baseType = typeDef.BaseType.ResolveGenericParameters(typeRef);
|
|
|
|
foreach (var tuple in baseType.GetMethods(predicate, module))
|
|
|
|
yield return tuple;
|
|
|
|
}
|
|
|
|
|
2016-08-02 23:24:50 +03:00
|
|
|
public static MethodReference GetImplicitOperatorTo(this TypeReference fromType, TypeReference toType, ModuleDefinition module)
|
2016-12-02 00:34:39 +03:00
|
|
|
{
|
|
|
|
var implicitOperatorsOnFromType = fromType.GetMethods(md => md.IsPublic && md.IsStatic && md.IsSpecialName && md.Name == "op_Implicit", module);
|
|
|
|
var implicitOperatorsOnToType = toType.GetMethods(md => md.IsPublic && md.IsStatic && md.IsSpecialName && md.Name == "op_Implicit", module);
|
|
|
|
var implicitOperators = implicitOperatorsOnFromType.Concat(implicitOperatorsOnToType).ToList();
|
2016-08-02 23:24:50 +03:00
|
|
|
if (implicitOperators.Any()) {
|
|
|
|
foreach (var op in implicitOperators) {
|
|
|
|
var cast = op.Item1;
|
|
|
|
var opDeclTypeRef = op.Item2;
|
|
|
|
var castDef = module.Import(cast).ResolveGenericParameters(opDeclTypeRef, module);
|
|
|
|
var returnType = castDef.ReturnType;
|
|
|
|
if (returnType.IsGenericParameter)
|
|
|
|
returnType = ((GenericInstanceType)opDeclTypeRef).GenericArguments [((GenericParameter)returnType).Position];
|
|
|
|
if (returnType.FullName == toType.FullName &&
|
|
|
|
cast.Parameters [0].ParameterType.Name == fromType.Name) {
|
|
|
|
return castDef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-03-22 23:02:25 +03:00
|
|
|
public static TypeReference ResolveGenericParameters(this TypeReference self, TypeReference declaringTypeReference)
|
|
|
|
{
|
|
|
|
var genericself = self as GenericInstanceType;
|
|
|
|
if (genericself == null)
|
|
|
|
return self;
|
|
|
|
|
|
|
|
var genericdeclType = declaringTypeReference as GenericInstanceType;
|
|
|
|
if (genericdeclType == null)
|
|
|
|
return self;
|
|
|
|
|
|
|
|
if (!genericself.GenericArguments.Any(arg => arg.IsGenericParameter))
|
|
|
|
return self;
|
|
|
|
|
|
|
|
List<TypeReference> args = new List<TypeReference>();
|
|
|
|
for (var i = 0; i < genericself.GenericArguments.Count; i++)
|
|
|
|
args.Add(genericdeclType.GenericArguments[(genericself.GenericArguments[i] as GenericParameter).Position]);
|
|
|
|
return self.GetElementType().MakeGenericInstanceType(args.ToArray());
|
|
|
|
}
|
|
|
|
}
|
2016-12-07 12:57:43 +03:00
|
|
|
}
|