Update mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/RelaxngPattern.cs
[mono.git] / mcs / class / IKVM.Reflection / MethodInfo.cs
1 /*
2   Copyright (C) 2009-2012 Jeroen Frijters
3
4   This software is provided 'as-is', without any express or implied
5   warranty.  In no event will the authors be held liable for any damages
6   arising from the use of this software.
7
8   Permission is granted to anyone to use this software for any purpose,
9   including commercial applications, and to alter it and redistribute it
10   freely, subject to the following restrictions:
11
12   1. The origin of this software must not be misrepresented; you must not
13      claim that you wrote the original software. If you use this software
14      in a product, an acknowledgment in the product documentation would be
15      appreciated but is not required.
16   2. Altered source versions must be plainly marked as such, and must not be
17      misrepresented as being the original software.
18   3. This notice may not be removed or altered from any source distribution.
19
20   Jeroen Frijters
21   jeroen@frijters.net
22   
23 */
24 using System;
25 using System.Collections.Generic;
26 using System.Diagnostics;
27 using System.Text;
28
29 namespace IKVM.Reflection
30 {
31         public abstract class MethodInfo : MethodBase, IGenericContext, IGenericBinder
32         {
33                 // prevent external subclasses
34                 internal MethodInfo()
35                 {
36                 }
37
38                 public sealed override MemberTypes MemberType
39                 {
40                         get { return MemberTypes.Method; }
41                 }
42
43                 public abstract Type ReturnType { get; }
44                 public abstract ParameterInfo ReturnParameter { get; }
45
46                 public virtual MethodInfo MakeGenericMethod(params Type[] typeArguments)
47                 {
48                         throw new NotSupportedException(this.GetType().FullName);
49                 }
50
51                 public virtual MethodInfo GetGenericMethodDefinition()
52                 {
53                         throw new NotSupportedException(this.GetType().FullName);
54                 }
55
56                 public override string ToString()
57                 {
58                         StringBuilder sb = new StringBuilder();
59                         sb.Append(this.ReturnType.Name).Append(' ').Append(this.Name);
60                         string sep;
61                         if (this.IsGenericMethod)
62                         {
63                                 sb.Append('[');
64                                 sep = "";
65                                 foreach (Type arg in GetGenericArguments())
66                                 {
67                                         sb.Append(sep).Append(arg);
68                                         sep = ", ";
69                                 }
70                                 sb.Append(']');
71                         }
72                         sb.Append('(');
73                         sep = "";
74                         foreach (ParameterInfo arg in GetParameters())
75                         {
76                                 sb.Append(sep).Append(arg.ParameterType);
77                                 sep = ", ";
78                         }
79                         sb.Append(')');
80                         return sb.ToString();
81                 }
82
83                 internal bool IsNewSlot
84                 {
85                         get { return (this.Attributes & MethodAttributes.NewSlot) != 0; }
86                 }
87
88                 public MethodInfo GetBaseDefinition()
89                 {
90                         MethodInfo match = this;
91                         if (match.IsVirtual)
92                         {
93                                 for (Type type = this.DeclaringType.BaseType; type != null && !match.IsNewSlot; type = type.BaseType)
94                                 {
95                                         MethodInfo method = type.FindMethod(this.Name, this.MethodSignature) as MethodInfo;
96                                         if (method != null && method.IsVirtual)
97                                         {
98                                                 match = method;
99                                         }
100                                 }
101                         }
102                         return match;
103                 }
104
105                 public virtual MethodInfo[] __GetMethodImpls()
106                 {
107                         throw new NotSupportedException();
108                 }
109
110                 Type IGenericContext.GetGenericTypeArgument(int index)
111                 {
112                         return this.DeclaringType.GetGenericTypeArgument(index);
113                 }
114
115                 Type IGenericContext.GetGenericMethodArgument(int index)
116                 {
117                         return GetGenericMethodArgument(index);
118                 }
119
120                 internal virtual Type GetGenericMethodArgument(int index)
121                 {
122                         throw new InvalidOperationException();
123                 }
124
125                 internal virtual int GetGenericMethodArgumentCount()
126                 {
127                         throw new InvalidOperationException();
128                 }
129
130                 internal override MethodInfo GetMethodOnTypeDefinition()
131                 {
132                         return this;
133                 }
134
135                 Type IGenericBinder.BindTypeParameter(Type type)
136                 {
137                         return this.DeclaringType.GetGenericTypeArgument(type.GenericParameterPosition);
138                 }
139
140                 Type IGenericBinder.BindMethodParameter(Type type)
141                 {
142                         return GetGenericMethodArgument(type.GenericParameterPosition);
143                 }
144
145                 internal override MethodBase BindTypeParameters(Type type)
146                 {
147                         return new GenericMethodInstance(this.DeclaringType.BindTypeParameters(type), this, null);
148                 }
149
150                 // This method is used by ILGenerator and exists to allow ArrayMethod to override it,
151                 // because ArrayMethod doesn't have a working MethodAttributes property, so it needs
152                 // to base the result of this on the CallingConvention.
153                 internal virtual bool HasThis
154                 {
155                         get { return !IsStatic; }
156                 }
157
158                 internal sealed override MemberInfo SetReflectedType(Type type)
159                 {
160                         return new MethodInfoWithReflectedType(type, this);
161                 }
162         }
163
164         sealed class MethodInfoWithReflectedType : MethodInfo
165         {
166                 private readonly Type reflectedType;
167                 private readonly MethodInfo method;
168
169                 internal MethodInfoWithReflectedType(Type reflectedType, MethodInfo method)
170                 {
171                         Debug.Assert(reflectedType != method.DeclaringType);
172                         this.reflectedType = reflectedType;
173                         this.method = method;
174                 }
175
176                 public override bool Equals(object obj)
177                 {
178                         MethodInfoWithReflectedType other = obj as MethodInfoWithReflectedType;
179                         return other != null
180                                 && other.reflectedType == reflectedType
181                                 && other.method == method;
182                 }
183
184                 public override int GetHashCode()
185                 {
186                         return reflectedType.GetHashCode() ^ method.GetHashCode();
187                 }
188
189                 internal override MethodSignature MethodSignature
190                 {
191                         get { return method.MethodSignature; }
192                 }
193
194                 internal override int ParameterCount
195                 {
196                         get { return method.ParameterCount; }
197                 }
198
199                 public override ParameterInfo[] GetParameters()
200                 {
201                         ParameterInfo[] parameters = method.GetParameters();
202                         for (int i = 0; i < parameters.Length; i++)
203                         {
204                                 parameters[i] = new ParameterInfoWrapper(this, parameters[i]);
205                         }
206                         return parameters;
207                 }
208
209                 public override MethodAttributes Attributes
210                 {
211                         get { return method.Attributes; }
212                 }
213
214                 public override MethodImplAttributes GetMethodImplementationFlags()
215                 {
216                         return method.GetMethodImplementationFlags();
217                 }
218
219                 public override MethodBody GetMethodBody()
220                 {
221                         return method.GetMethodBody();
222                 }
223
224                 public override CallingConventions CallingConvention
225                 {
226                         get { return method.CallingConvention; }
227                 }
228
229                 public override int __MethodRVA
230                 {
231                         get { return method.__MethodRVA; }
232                 }
233
234                 public override Type ReturnType
235                 {
236                         get { return method.ReturnType; }
237                 }
238
239                 public override ParameterInfo ReturnParameter
240                 {
241                         get { return new ParameterInfoWrapper(this, method.ReturnParameter); }
242                 }
243
244                 public override MethodInfo MakeGenericMethod(params Type[] typeArguments)
245                 {
246                         return SetReflectedType(method.MakeGenericMethod(typeArguments), reflectedType);
247                 }
248
249                 public override MethodInfo GetGenericMethodDefinition()
250                 {
251                         return method.GetGenericMethodDefinition();
252                 }
253
254                 public override string ToString()
255                 {
256                         return method.ToString();
257                 }
258
259                 public override MethodInfo[] __GetMethodImpls()
260                 {
261                         return method.__GetMethodImpls();
262                 }
263
264                 internal override Type GetGenericMethodArgument(int index)
265                 {
266                         return method.GetGenericMethodArgument(index);
267                 }
268
269                 internal override int GetGenericMethodArgumentCount()
270                 {
271                         return method.GetGenericMethodArgumentCount();
272                 }
273
274                 internal override MethodInfo GetMethodOnTypeDefinition()
275                 {
276                         return method.GetMethodOnTypeDefinition();
277                 }
278
279                 internal override bool HasThis
280                 {
281                         get { return method.HasThis; }
282                 }
283
284                 public override Module Module
285                 {
286                         get { return method.Module; }
287                 }
288
289                 public override Type DeclaringType
290                 {
291                         get { return method.DeclaringType; }
292                 }
293
294                 public override Type ReflectedType
295                 {
296                         get { return reflectedType; }
297                 }
298
299                 public override string Name
300                 {
301                         get { return method.Name; }
302                 }
303
304                 internal override int ImportTo(IKVM.Reflection.Emit.ModuleBuilder module)
305                 {
306                         return method.ImportTo(module);
307                 }
308
309                 public override MethodBase __GetMethodOnTypeDefinition()
310                 {
311                         return method.__GetMethodOnTypeDefinition();
312                 }
313
314                 public override bool __IsMissing
315                 {
316                         get { return method.__IsMissing; }
317                 }
318
319                 internal override MethodBase BindTypeParameters(Type type)
320                 {
321                         return method.BindTypeParameters(type);
322                 }
323
324                 public override bool ContainsGenericParameters
325                 {
326                         get { return method.ContainsGenericParameters; }
327                 }
328
329                 internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
330                 {
331                         return method.GetCustomAttributesData(attributeType);
332                 }
333
334                 public override Type[] GetGenericArguments()
335                 {
336                         return method.GetGenericArguments();
337                 }
338
339                 public override bool IsGenericMethod
340                 {
341                         get { return method.IsGenericMethod; }
342                 }
343
344                 public override bool IsGenericMethodDefinition
345                 {
346                         get { return method.IsGenericMethodDefinition; }
347                 }
348
349                 public override int MetadataToken
350                 {
351                         get { return method.MetadataToken; }
352                 }
353         }
354 }