summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Build.Tasks/TypeReferenceExtensions.cs
blob: 60e52c801efb1dbd5c18c386ad87fc8c9d0337ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;
using Mono.Cecil.Rocks;

namespace Xamarin.Forms.Build.Tasks
{
	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;
			if (
				(iface =
					typeDef.Interfaces.FirstOrDefault(
						tr =>
							tr.FullName.StartsWith(@interface) && tr.IsGenericInstance && (tr as GenericInstanceType).HasGenericArguments)) !=
				null)
			{
				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;
		}

		public static bool InheritsFromOrImplements(this TypeReference typeRef, TypeReference baseClass)
		{
			var arrayInterfaces = new[]
			{
				"System.IEnumerable",
				"System.Collections.IList",
				"System.Collections.Collection"
			};

			var arrayGenericInterfaces = new[]
			{
				"System.IEnumerable`1",
				"System.Collections.Generic.IList`1",
				"System.Collections.Generic.IReadOnlyCollection<T>",
				"System.Collections.Generic.IReadOnlyList<T>",
				"System.Collections.Generic.Collection<T>"
			};

			if (typeRef.IsArray)
			{
				var arrayType = typeRef.Resolve();
				if (arrayInterfaces.Contains(baseClass.FullName))
					return true;
				if (arrayGenericInterfaces.Contains(baseClass.Resolve().FullName) &&
				    baseClass.IsGenericInstance &&
				    (baseClass as GenericInstanceType).GenericArguments[0].FullName == arrayType.FullName)
					return true;
			}
			var typeDef = typeRef.Resolve();
			if (typeDef.FullName == baseClass.FullName)
				return true;
			if (typeDef.Interfaces.Any(ir => ir.FullName == baseClass.FullName))
				return true;
			if (typeDef.FullName == "System.Object")
				return false;
			if (typeDef.BaseType == null)
				return false;
			return typeDef.BaseType.InheritsFromOrImplements(baseClass);
		}

		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;
		}

		public static MethodReference GetImplicitOperatorTo(this TypeReference fromType, TypeReference toType, ModuleDefinition module)
		{ 
			var implicitOperators = fromType.GetMethods(md => md.IsPublic && md.IsStatic && md.IsSpecialName && md.Name == "op_Implicit",
												module).ToList();
			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;
		}

		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());
		}
	}
}