PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/boo/boo-lang
C# | 336 lines | 251 code | 54 blank | 31 comment | 20 complexity | 66a5ebcff10b8b1199210fcd8eb5e788 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. using System;
  29. using Boo.Lang.Compiler.Ast;
  30. using System.Collections.Generic;
  31. namespace Boo.Lang.Compiler.TypeSystem
  32. {
  33. /// <summary>
  34. /// A generic type parameter of an internal generic type or method.
  35. /// </summary>
  36. public class InternalGenericParameter : IType, IInternalEntity, IGenericParameter
  37. {
  38. int _position = -1;
  39. GenericParameterDeclaration _declaration;
  40. TypeSystemServices _tss;
  41. IType[] _baseTypes = null;
  42. public InternalGenericParameter(TypeSystemServices tss, GenericParameterDeclaration declaration)
  43. {
  44. _tss = tss;
  45. _declaration = declaration;
  46. }
  47. public InternalGenericParameter(TypeSystemServices tss, GenericParameterDeclaration declaration, int position)
  48. : this(tss, declaration)
  49. {
  50. _position = position;
  51. }
  52. public int GenericParameterPosition
  53. {
  54. get
  55. {
  56. if (_position == -1)
  57. {
  58. IGenericParameter[] parameters =
  59. DeclaringMethod != null ? DeclaringMethod.GenericInfo.GenericParameters : DeclaringType.GenericInfo.GenericParameters;
  60. _position = Array.IndexOf(parameters, this);
  61. }
  62. return _position;
  63. }
  64. }
  65. public bool MustHaveDefaultConstructor
  66. {
  67. get
  68. {
  69. return HasConstraint(GenericParameterConstraints.Constructable);
  70. }
  71. }
  72. public Variance Variance
  73. {
  74. get
  75. {
  76. if (HasConstraint(GenericParameterConstraints.Covariant))
  77. {
  78. return Variance.Covariant;
  79. }
  80. else if (HasConstraint(GenericParameterConstraints.Contravariant))
  81. {
  82. return Variance.Contravariant;
  83. }
  84. return Variance.Invariant;
  85. }
  86. }
  87. public IType[] GetTypeConstraints()
  88. {
  89. if (_baseTypes == null)
  90. {
  91. List<IType> baseTypes = new List<IType>();
  92. foreach (TypeReference baseTypeReference in _declaration.BaseTypes)
  93. {
  94. IType baseType = (IType)baseTypeReference.Entity;
  95. if (baseType != null)
  96. {
  97. baseTypes.Add(baseType);
  98. }
  99. }
  100. _baseTypes = baseTypes.ToArray();
  101. }
  102. return _baseTypes;
  103. }
  104. public bool IsValueType
  105. {
  106. get { return HasConstraint(GenericParameterConstraints.ValueType); }
  107. }
  108. public bool IsClass
  109. {
  110. get { return HasConstraint(GenericParameterConstraints.ReferenceType); }
  111. }
  112. public IType DeclaringType
  113. {
  114. get
  115. {
  116. return DeclaringEntity as IType ??
  117. ((IMethod)DeclaringEntity).DeclaringType;
  118. }
  119. }
  120. public IMethod DeclaringMethod
  121. {
  122. get { return DeclaringEntity as IMethod; }
  123. }
  124. public IEntity DeclaringEntity
  125. {
  126. get
  127. {
  128. return TypeSystemServices.GetEntity(_declaration.ParentNode);
  129. }
  130. }
  131. bool IType.IsAbstract
  132. {
  133. get { return false; }
  134. }
  135. bool IType.IsInterface
  136. {
  137. get { return false; }
  138. }
  139. bool IType.IsEnum
  140. {
  141. get { return false; }
  142. }
  143. public bool IsByRef
  144. {
  145. get { return false; }
  146. }
  147. bool IType.IsFinal
  148. {
  149. get { return true; }
  150. }
  151. bool IType.IsArray
  152. {
  153. get { return false; }
  154. }
  155. public int GetTypeDepth()
  156. {
  157. return DeclaringType.GetTypeDepth() + 1;
  158. }
  159. IType IType.GetElementType()
  160. {
  161. return null;
  162. }
  163. public IType BaseType
  164. {
  165. get { return FindBaseType(); }
  166. }
  167. public IEntity GetDefaultMember()
  168. {
  169. return null;
  170. }
  171. public IConstructor[] GetConstructors()
  172. {
  173. if (MustHaveDefaultConstructor)
  174. {
  175. // TODO: return a something implementing IConstructor...?
  176. }
  177. return null;
  178. }
  179. public IType[] GetInterfaces()
  180. {
  181. List<IType> interfaces = new List<IType>();
  182. foreach (IType type in GetTypeConstraints())
  183. {
  184. if (type.IsInterface)
  185. {
  186. interfaces.Add(type);
  187. }
  188. }
  189. return interfaces.ToArray();
  190. }
  191. public bool IsSubclassOf(IType other)
  192. {
  193. return (other == BaseType || BaseType.IsSubclassOf(other));
  194. }
  195. public bool IsAssignableFrom(IType other)
  196. {
  197. if (other == this)
  198. {
  199. return true;
  200. }
  201. if (other == Null.Default)
  202. {
  203. return IsClass;
  204. }
  205. return false;
  206. }
  207. IGenericTypeInfo IType.GenericInfo
  208. {
  209. get { return null; }
  210. }
  211. IConstructedTypeInfo IType.ConstructedInfo
  212. {
  213. get { return null; }
  214. }
  215. public string Name
  216. {
  217. get { return _declaration.Name; }
  218. }
  219. public string FullName
  220. {
  221. get
  222. {
  223. return string.Format("{0}.{1}", DeclaringEntity.FullName, Name);
  224. }
  225. }
  226. public EntityType EntityType
  227. {
  228. get { return EntityType.Type; }
  229. }
  230. public IType Type
  231. {
  232. get { return this; }
  233. }
  234. INamespace INamespace.ParentNamespace
  235. {
  236. get { return (INamespace)DeclaringEntity; }
  237. }
  238. IEntity[] INamespace.GetMembers()
  239. {
  240. return NullNamespace.EmptyEntityArray;
  241. }
  242. bool INamespace.Resolve(List targetList, string name, EntityType flags)
  243. {
  244. bool resolved = false;
  245. // Resolve using base type constraints
  246. foreach (IType type in GetTypeConstraints())
  247. {
  248. resolved |= type.Resolve(targetList, name, flags);
  249. }
  250. // Resolve using System.Object
  251. resolved |= _tss.ObjectType.Resolve(targetList, name, flags);
  252. return resolved;
  253. }
  254. public Node Node
  255. {
  256. get { return _declaration; }
  257. }
  258. override public string ToString()
  259. {
  260. return Name;
  261. }
  262. bool IEntityWithAttributes.IsDefined(IType attributeType)
  263. {
  264. throw new NotImplementedException();
  265. }
  266. private bool HasConstraint(GenericParameterConstraints flag)
  267. {
  268. return (_declaration.Constraints & flag) == flag;
  269. }
  270. private IType FindBaseType()
  271. {
  272. foreach (IType type in GetTypeConstraints())
  273. {
  274. if (!type.IsInterface)
  275. {
  276. return type;
  277. }
  278. }
  279. return _tss.ObjectType;
  280. }
  281. }
  282. }