PageRenderTime 58ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Boo.Lang.Compiler/TypeSystem/ExternalMethod.cs

https://github.com/boo/boo-lang
C# | 316 lines | 252 code | 38 blank | 26 comment | 36 complexity | 43b6547c74438ddfcfe1a17b8a720479 MD5 | raw file
Possible License(s): GPL-2.0
  1. #region license
  2. // Copyright (c) 2004, Rodrigo B. de Oliveira (rbo@acm.org)
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without modification,
  6. // are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Rodrigo B. de Oliveira nor the names of its
  14. // contributors may be used to endorse or promote products derived from this
  15. // software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  21. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #endregion
  28. namespace Boo.Lang.Compiler.TypeSystem
  29. {
  30. using System;
  31. using System.Reflection;
  32. public class ExternalMethod : ExternalEntity<MethodBase>, IMethod
  33. {
  34. protected IParameter[] _parameters;
  35. protected ICallableType _type;
  36. private bool? _acceptVarArgs;
  37. private bool? _isBooExtension;
  38. private bool? _isClrExtension;
  39. private bool? _isPInvoke;
  40. private bool? _isMeta;
  41. internal ExternalMethod(TypeSystemServices manager, MethodBase mi) : base(manager, mi)
  42. {
  43. }
  44. public bool IsMeta
  45. {
  46. get
  47. {
  48. if (null == _isMeta)
  49. {
  50. _isMeta = IsStatic && MetadataUtil.IsAttributeDefined(_memberInfo, typeof(Boo.Lang.MetaAttribute));
  51. }
  52. return _isMeta.Value;
  53. }
  54. }
  55. public bool IsExtension
  56. {
  57. get
  58. {
  59. return IsBooExtension || IsClrExtension;
  60. }
  61. }
  62. public bool IsBooExtension
  63. {
  64. get
  65. {
  66. if (null == _isBooExtension)
  67. {
  68. _isBooExtension = MetadataUtil.IsAttributeDefined(_memberInfo, Types.BooExtensionAttribute);
  69. }
  70. return _isBooExtension.Value;
  71. }
  72. }
  73. public bool IsClrExtension
  74. {
  75. get
  76. {
  77. if (null == _isClrExtension)
  78. {
  79. _isClrExtension = MetadataUtil.HasClrExtensions()
  80. && IsStatic
  81. && MetadataUtil.IsAttributeDefined(_memberInfo, Types.ClrExtensionAttribute);
  82. }
  83. return _isClrExtension.Value;
  84. }
  85. }
  86. public bool IsPInvoke
  87. {
  88. get
  89. {
  90. if (null == _isPInvoke)
  91. {
  92. _isPInvoke = IsStatic && MetadataUtil.IsAttributeDefined(_memberInfo, Types.DllImportAttribute);
  93. }
  94. return _isPInvoke.Value;
  95. }
  96. }
  97. public virtual IType DeclaringType
  98. {
  99. get
  100. {
  101. return _typeSystemServices.Map(_memberInfo.DeclaringType);
  102. }
  103. }
  104. public bool IsStatic
  105. {
  106. get
  107. {
  108. return _memberInfo.IsStatic;
  109. }
  110. }
  111. public bool IsPublic
  112. {
  113. get
  114. {
  115. return _memberInfo.IsPublic;
  116. }
  117. }
  118. public bool IsProtected
  119. {
  120. get
  121. {
  122. return _memberInfo.IsFamily || _memberInfo.IsFamilyOrAssembly;
  123. }
  124. }
  125. public bool IsPrivate
  126. {
  127. get
  128. {
  129. return _memberInfo.IsPrivate;
  130. }
  131. }
  132. public bool IsAbstract
  133. {
  134. get
  135. {
  136. return _memberInfo.IsAbstract;
  137. }
  138. }
  139. public bool IsInternal
  140. {
  141. get
  142. {
  143. return _memberInfo.IsAssembly;
  144. }
  145. }
  146. public bool IsVirtual
  147. {
  148. get
  149. {
  150. return _memberInfo.IsVirtual;
  151. }
  152. }
  153. public bool IsSpecialName
  154. {
  155. get
  156. {
  157. return _memberInfo.IsSpecialName;
  158. }
  159. }
  160. public bool AcceptVarArgs
  161. {
  162. get
  163. {
  164. if (null == _acceptVarArgs)
  165. {
  166. ParameterInfo[] parameters = _memberInfo.GetParameters();
  167. _acceptVarArgs =
  168. parameters.Length > 0 && IsParamArray(parameters[parameters.Length-1]);
  169. }
  170. return _acceptVarArgs.Value;
  171. }
  172. }
  173. private bool IsParamArray(ParameterInfo parameter)
  174. {
  175. /* Hack to fix problem with mono-1.1.8.* and older */
  176. return parameter.ParameterType.IsArray
  177. && (
  178. Attribute.IsDefined(parameter, Types.ParamArrayAttribute)
  179. || parameter.GetCustomAttributes(Types.ParamArrayAttribute, false).Length > 0);
  180. }
  181. override public EntityType EntityType
  182. {
  183. get
  184. {
  185. return EntityType.Method;
  186. }
  187. }
  188. public ICallableType CallableType
  189. {
  190. get
  191. {
  192. if (null != _type) return _type;
  193. return _type = _typeSystemServices.GetCallableType(this);
  194. }
  195. }
  196. public IType Type
  197. {
  198. get
  199. {
  200. return CallableType;
  201. }
  202. }
  203. public virtual IParameter[] GetParameters()
  204. {
  205. if (null != _parameters) return _parameters;
  206. return _parameters = _typeSystemServices.Map(_memberInfo.GetParameters());
  207. }
  208. public virtual IType ReturnType
  209. {
  210. get
  211. {
  212. MethodInfo mi = _memberInfo as MethodInfo;
  213. if (null != mi) return _typeSystemServices.Map(mi.ReturnType);
  214. return null;
  215. }
  216. }
  217. public MethodBase MethodInfo
  218. {
  219. get { return _memberInfo; }
  220. }
  221. override public bool Equals(object other)
  222. {
  223. ExternalMethod rhs = other as ExternalMethod;
  224. if (null == rhs) return false;
  225. return _memberInfo.MethodHandle.Value == rhs._memberInfo.MethodHandle.Value;
  226. }
  227. override public int GetHashCode()
  228. {
  229. return _memberInfo.MethodHandle.Value.GetHashCode();
  230. }
  231. override public string ToString()
  232. {
  233. return _typeSystemServices.GetSignature(this);
  234. }
  235. ExternalGenericMethodInfo _genericMethodDefinitionInfo = null;
  236. public IGenericMethodInfo GenericInfo
  237. {
  238. get
  239. {
  240. if (MethodInfo.IsGenericMethodDefinition)
  241. {
  242. if (_genericMethodDefinitionInfo == null)
  243. {
  244. _genericMethodDefinitionInfo =
  245. new ExternalGenericMethodInfo(_typeSystemServices, this);
  246. }
  247. return _genericMethodDefinitionInfo;
  248. }
  249. return null;
  250. }
  251. }
  252. ExternalConstructedMethodInfo _genericMethodInfo = null;
  253. public virtual IConstructedMethodInfo ConstructedInfo
  254. {
  255. get
  256. {
  257. if (MethodInfo.IsGenericMethod)
  258. {
  259. if (_genericMethodInfo == null)
  260. {
  261. _genericMethodInfo = new ExternalConstructedMethodInfo(_typeSystemServices, this);
  262. }
  263. return _genericMethodInfo;
  264. }
  265. return null;
  266. }
  267. }
  268. protected override Type MemberType
  269. {
  270. get
  271. {
  272. MethodInfo mi = _memberInfo as MethodInfo;
  273. if (null != mi) return mi.ReturnType;
  274. return null;
  275. }
  276. }
  277. }
  278. }