MethodReference.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //
  2. // MethodReference.cs
  3. //
  4. // Author:
  5. // Jb Evain (jbevain@gmail.com)
  6. //
  7. // Copyright (c) 2008 - 2011 Jb Evain
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Text;
  30. using Mono.Collections.Generic;
  31. namespace Mono.Cecil
  32. {
  33. public class MethodReference : MemberReference, IMethodSignature, IGenericParameterProvider, IGenericContext
  34. {
  35. int hashCode = -1;
  36. static int instance_id;
  37. internal ParameterDefinitionCollection parameters;
  38. MethodReturnType return_type;
  39. bool has_this;
  40. bool explicit_this;
  41. MethodCallingConvention calling_convention;
  42. internal Collection<GenericParameter> generic_parameters;
  43. public virtual bool HasThis
  44. {
  45. get { return has_this; }
  46. set { has_this = value; }
  47. }
  48. public virtual bool ExplicitThis
  49. {
  50. get { return explicit_this; }
  51. set { explicit_this = value; }
  52. }
  53. public virtual MethodCallingConvention CallingConvention
  54. {
  55. get { return calling_convention; }
  56. set { calling_convention = value; }
  57. }
  58. public virtual bool HasParameters
  59. {
  60. get { return !Mixin.IsNullOrEmpty(parameters); }
  61. }
  62. public virtual Collection<ParameterDefinition> Parameters
  63. {
  64. get
  65. {
  66. if (parameters == null)
  67. parameters = new ParameterDefinitionCollection(this);
  68. return parameters;
  69. }
  70. }
  71. IGenericParameterProvider IGenericContext.Type
  72. {
  73. get
  74. {
  75. var declaring_type = this.DeclaringType;
  76. var instance = declaring_type as GenericInstanceType;
  77. if (instance != null)
  78. return instance.ElementType;
  79. return declaring_type;
  80. }
  81. }
  82. IGenericParameterProvider IGenericContext.Method
  83. {
  84. get { return this; }
  85. }
  86. GenericParameterType IGenericParameterProvider.GenericParameterType
  87. {
  88. get { return GenericParameterType.Method; }
  89. }
  90. public virtual bool HasGenericParameters
  91. {
  92. get { return !Mixin.IsNullOrEmpty(generic_parameters); }
  93. }
  94. public virtual Collection<GenericParameter> GenericParameters
  95. {
  96. get
  97. {
  98. if (generic_parameters != null)
  99. return generic_parameters;
  100. return generic_parameters = new GenericParameterCollection(this);
  101. }
  102. }
  103. public TypeReference ReturnType
  104. {
  105. get
  106. {
  107. var return_type = MethodReturnType;
  108. return return_type != null ? return_type.ReturnType : null;
  109. }
  110. set
  111. {
  112. var return_type = MethodReturnType;
  113. if (return_type != null)
  114. return_type.ReturnType = value;
  115. }
  116. }
  117. public virtual MethodReturnType MethodReturnType
  118. {
  119. get { return return_type; }
  120. set { return_type = value; }
  121. }
  122. public override string FullName
  123. {
  124. get
  125. {
  126. var builder = new StringBuilder();
  127. builder.Append(ReturnType.FullName)
  128. .Append(" ")
  129. .Append(MemberFullName());
  130. Mixin.MethodSignatureFullName(this, builder);
  131. return builder.ToString();
  132. }
  133. }
  134. public override int GetHashCode()
  135. {
  136. if (hashCode == -1)
  137. hashCode = System.Threading.Interlocked.Add(ref instance_id, 1);
  138. return hashCode;
  139. }
  140. public virtual bool IsGenericInstance
  141. {
  142. get { return false; }
  143. }
  144. internal override bool ContainsGenericParameter
  145. {
  146. get
  147. {
  148. if (this.ReturnType.ContainsGenericParameter || base.ContainsGenericParameter)
  149. return true;
  150. var parameters = this.Parameters;
  151. for (int i = 0; i < parameters.Count; i++)
  152. if (parameters[i].ParameterType.ContainsGenericParameter)
  153. return true;
  154. return false;
  155. }
  156. }
  157. internal MethodReference()
  158. {
  159. this.return_type = new MethodReturnType(this);
  160. this.token = new MetadataToken(TokenType.MemberRef);
  161. }
  162. public MethodReference(string name, TypeReference returnType)
  163. : base(name)
  164. {
  165. if (returnType == null)
  166. throw new ArgumentNullException("returnType");
  167. this.return_type = new MethodReturnType(this);
  168. this.return_type.ReturnType = returnType;
  169. this.token = new MetadataToken(TokenType.MemberRef);
  170. }
  171. public MethodReference(string name, TypeReference returnType, TypeReference declaringType)
  172. : this(name, returnType)
  173. {
  174. if (declaringType == null)
  175. throw new ArgumentNullException("declaringType");
  176. this.DeclaringType = declaringType;
  177. }
  178. public virtual MethodReference GetElementMethod()
  179. {
  180. return this;
  181. }
  182. public virtual MethodDefinition Resolve()
  183. {
  184. var module = this.Module;
  185. if (module == null)
  186. throw new NotSupportedException();
  187. return module.Resolve(this);
  188. }
  189. }
  190. static partial class Mixin
  191. {
  192. public static bool IsVarArg(IMethodSignature self)
  193. {
  194. return (self.CallingConvention & MethodCallingConvention.VarArg) != 0;
  195. }
  196. public static int GetSentinelPosition(IMethodSignature self)
  197. {
  198. if (!self.HasParameters)
  199. return -1;
  200. var parameters = self.Parameters;
  201. for (int i = 0; i < parameters.Count; i++)
  202. if (parameters[i].ParameterType.IsSentinel)
  203. return i;
  204. return -1;
  205. }
  206. }
  207. }