PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/boo/boo-lang
C# | 428 lines | 299 code | 76 blank | 53 comment | 2 complexity | 5ae2bfac7b86e9ad859019d3375e7737 MD5 | raw file
Possible License(s): GPL-2.0
  1. #region license
  2. // Copyright (c) 2003, 2004, 2005 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.Collections.Generic;
  32. using Boo.Lang.Compiler.TypeSystem;
  33. using Boo.Lang.Compiler.Ast;
  34. /// <summary>
  35. /// A base class for a member mapped from a generic type onto a constructed type.
  36. /// </summary>
  37. public abstract class GenericMappedMember<T> : IMember where T : IMember
  38. {
  39. protected readonly TypeSystemServices _tss;
  40. readonly T _source;
  41. readonly GenericMapping _genericMapping;
  42. string _fullName = null;
  43. protected GenericMappedMember(TypeSystemServices tss, T source, GenericMapping genericMapping)
  44. {
  45. _tss = tss;
  46. _source = source;
  47. _genericMapping = genericMapping;
  48. }
  49. public T Source
  50. {
  51. get { return _source; }
  52. }
  53. private string BuildFullName()
  54. {
  55. return DeclaringType.FullName + "." + Name;
  56. }
  57. public GenericMapping GenericMapping
  58. {
  59. get { return _genericMapping; }
  60. }
  61. public bool IsDuckTyped
  62. {
  63. get { return Source.IsDuckTyped; }
  64. }
  65. public IType DeclaringType
  66. {
  67. get { return GenericMapping.Map(Source.DeclaringType); }
  68. }
  69. public bool IsStatic
  70. {
  71. get { return Source.IsStatic; }
  72. }
  73. public IType Type
  74. {
  75. get { return GenericMapping.Map(Source.Type); }
  76. }
  77. public EntityType EntityType
  78. {
  79. get { return Source.EntityType; }
  80. }
  81. public string Name
  82. {
  83. get
  84. {
  85. return Source.Name;
  86. }
  87. }
  88. public string FullName
  89. {
  90. get { return _fullName ?? (_fullName = BuildFullName()); }
  91. }
  92. public bool IsPublic
  93. {
  94. get { return Source.IsPublic; }
  95. }
  96. public override string ToString()
  97. {
  98. return FullName;
  99. }
  100. public bool IsDefined(IType attributeType)
  101. {
  102. return _source.IsDefined(attributeType);
  103. }
  104. }
  105. /// <summary>
  106. /// A base class for an accessible member mapped from a generic type onto a constructed type.
  107. /// </summary>
  108. /// <typeparam name="T"></typeparam>
  109. public abstract class GenericMappedAccessibleMember<T> : GenericMappedMember<T> where T : IAccessibleMember
  110. {
  111. protected GenericMappedAccessibleMember(TypeSystemServices tss, T source, GenericMapping genericMapping)
  112. : base(tss, source, genericMapping)
  113. {
  114. }
  115. public bool IsProtected
  116. {
  117. get { return Source.IsProtected; }
  118. }
  119. public bool IsInternal
  120. {
  121. get { return Source.IsInternal; }
  122. }
  123. public bool IsPrivate
  124. {
  125. get { return Source.IsPrivate; }
  126. }
  127. }
  128. #region class GenericMappedMethod
  129. /// <summary>
  130. /// A method on a generic constructed type.
  131. /// </summary>
  132. public class GenericMappedMethod : GenericMappedAccessibleMember<IMethod>, IMethod
  133. {
  134. IParameter[] _parameters = null;
  135. ICallableType _callableType = null;
  136. public GenericMappedMethod(TypeSystemServices tss, IMethod source, GenericMapping genericMapping)
  137. : base(tss, source, genericMapping)
  138. {
  139. }
  140. public bool IsAbstract
  141. {
  142. get { return Source.IsAbstract; }
  143. }
  144. public bool IsVirtual
  145. {
  146. get { return Source.IsVirtual; }
  147. }
  148. public bool IsSpecialName
  149. {
  150. get { return Source.IsSpecialName; }
  151. }
  152. public bool IsPInvoke
  153. {
  154. get { return Source.IsPInvoke; }
  155. }
  156. public virtual IConstructedMethodInfo ConstructedInfo
  157. {
  158. // Generic mapped methods are not generic methods - those are InternalGenericMethods
  159. get { return null; }
  160. }
  161. public IGenericMethodInfo GenericInfo
  162. {
  163. // TODO: Generic mapped methods can be generic definitions!
  164. get { return null; }
  165. }
  166. public ICallableType CallableType
  167. {
  168. get
  169. {
  170. if (null == _callableType)
  171. {
  172. _callableType = _tss.GetCallableType(this);
  173. }
  174. return _callableType;
  175. }
  176. }
  177. public bool AcceptVarArgs
  178. {
  179. get { return Source.AcceptVarArgs; }
  180. }
  181. public bool IsExtension
  182. {
  183. get { return Source.IsExtension; }
  184. }
  185. public bool IsBooExtension
  186. {
  187. get { return Source.IsBooExtension; }
  188. }
  189. public bool IsClrExtension
  190. {
  191. get { return Source.IsClrExtension; }
  192. }
  193. public IType ReturnType
  194. {
  195. get { return GenericMapping.Map(Source.ReturnType); }
  196. }
  197. public IParameter[] GetParameters()
  198. {
  199. return _parameters ?? (_parameters = GenericMapping.Map(Source.GetParameters()));
  200. }
  201. }
  202. #endregion
  203. #region class GenericMappedConstructor
  204. /// <summary>
  205. /// A constructor on a generic constructed type.
  206. /// </summary>
  207. public class GenericMappedConstructor : GenericMappedMethod, IConstructor
  208. {
  209. public GenericMappedConstructor(TypeSystemServices tss, IConstructor source, GenericMapping genericMapping)
  210. : base(tss, (IMethod)source, genericMapping)
  211. {
  212. }
  213. }
  214. #endregion
  215. #region class GenericMappedProperty
  216. /// <summary>
  217. /// A property on a generic constructed type.
  218. /// </summary>
  219. public class GenericMappedProperty : GenericMappedAccessibleMember<IProperty>, IProperty
  220. {
  221. IParameter[] _parameters;
  222. public GenericMappedProperty(TypeSystemServices tss, IProperty source, GenericMapping genericMapping)
  223. : base(tss, source, genericMapping)
  224. {
  225. }
  226. public IParameter[] GetParameters()
  227. {
  228. return _parameters ?? (_parameters = GenericMapping.Map(Source.GetParameters()));
  229. }
  230. public IMethod GetGetMethod()
  231. {
  232. return GenericMapping.Map(Source.GetGetMethod());
  233. }
  234. public IMethod GetSetMethod()
  235. {
  236. return GenericMapping.Map(Source.GetSetMethod());
  237. }
  238. public override string ToString()
  239. {
  240. return string.Format("{0} as {1}", Name, Type);
  241. }
  242. public bool AcceptVarArgs
  243. {
  244. get { return Source.AcceptVarArgs; }
  245. }
  246. public bool IsExtension
  247. {
  248. get { return Source.IsExtension; }
  249. }
  250. public bool IsBooExtension
  251. {
  252. get { return Source.IsBooExtension; }
  253. }
  254. public bool IsClrExtension
  255. {
  256. get { return Source.IsClrExtension; }
  257. }
  258. }
  259. #endregion
  260. #region class GenericMappedEvent
  261. /// <summary>
  262. /// An event in a constructed generic type.
  263. /// </summary>
  264. public class GenericMappedEvent : GenericMappedMember<IEvent>, IEvent
  265. {
  266. public GenericMappedEvent(TypeSystemServices tss, IEvent source, GenericMapping genericMapping)
  267. : base(tss, source, genericMapping)
  268. {
  269. }
  270. public IMethod GetAddMethod()
  271. {
  272. return GenericMapping.Map(Source.GetAddMethod());
  273. }
  274. public IMethod GetRemoveMethod()
  275. {
  276. return GenericMapping.Map(Source.GetRemoveMethod());
  277. }
  278. public IMethod GetRaiseMethod()
  279. {
  280. return GenericMapping.Map(Source.GetRemoveMethod());
  281. }
  282. public bool IsAbstract
  283. {
  284. get { return Source.IsAbstract; }
  285. }
  286. public bool IsVirtual
  287. {
  288. get { return Source.IsVirtual; }
  289. }
  290. }
  291. #endregion
  292. #region class GenericMappedField
  293. /// <summary>
  294. /// A field on a generic constructed type.
  295. /// </summary>
  296. public class GenericMappedField : GenericMappedAccessibleMember<IField>, IField
  297. {
  298. public GenericMappedField(TypeSystemServices tss, IField source, GenericMapping genericMapping)
  299. : base(tss, source, genericMapping)
  300. {
  301. }
  302. public bool IsInitOnly
  303. {
  304. get { return Source.IsInitOnly; }
  305. }
  306. public bool IsLiteral
  307. {
  308. get { return Source.IsLiteral; }
  309. }
  310. public object StaticValue
  311. {
  312. get { return Source.StaticValue; }
  313. }
  314. }
  315. #endregion
  316. #region class GenericMappedParameter
  317. /// <summary>
  318. /// A parameter in a method constructed from a generic method, or a mapped onto a type constructed
  319. /// from a generic type.
  320. /// </summary>
  321. public class GenericMappedParameter : IParameter
  322. {
  323. private GenericMapping _genericMapping;
  324. private IParameter _baseParameter;
  325. public GenericMappedParameter(IParameter parameter, GenericMapping genericMapping)
  326. {
  327. _genericMapping = genericMapping;
  328. _baseParameter = parameter;
  329. }
  330. public bool IsByRef
  331. {
  332. get { return _baseParameter.IsByRef; }
  333. }
  334. public IType Type
  335. {
  336. get { return _genericMapping.Map(_baseParameter.Type); }
  337. }
  338. public string Name
  339. {
  340. get { return _baseParameter.Name; }
  341. }
  342. public string FullName
  343. {
  344. get { return _baseParameter.FullName; }
  345. }
  346. public EntityType EntityType
  347. {
  348. get { return EntityType.Parameter; }
  349. }
  350. }
  351. #endregion
  352. }