PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/w4x/boolangstudio
C# | 306 lines | 217 code | 50 blank | 39 comment | 22 complexity | 479551aca49e0869f50f88820a4d1b6a 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 System.Reflection;
  30. using Boo.Lang.Compiler;
  31. using Boo.Lang.Compiler.Ast;
  32. namespace Boo.Lang.Compiler.TypeSystem
  33. {
  34. /// <summary>
  35. /// A generic type parameter of an internal generic type or method.
  36. /// </summary>
  37. public class InternalGenericParameter : IType, IInternalEntity, IGenericParameter
  38. {
  39. TypeSystemServices _tss;
  40. int _position = -1;
  41. TypeDefinition _declaringType;
  42. Method _declaringMethod;
  43. GenericParameterDeclaration _declaration;
  44. IType[] _emptyTypeArray = new IType[0];
  45. public InternalGenericParameter(TypeSystemServices tss, GenericParameterDeclaration declaration)
  46. {
  47. _tss = tss;
  48. _declaration = declaration;
  49. // Determine and remember declaring type and declaring method (if applicable)
  50. _declaringMethod = declaration.ParentNode as Method;
  51. _declaringType = (
  52. _declaringMethod == null ?
  53. declaration.ParentNode as TypeDefinition : _declaringMethod.DeclaringType);
  54. }
  55. public InternalGenericParameter(TypeSystemServices tss, GenericParameterDeclaration declaration, int position)
  56. : this(tss, declaration)
  57. {
  58. _position = position;
  59. }
  60. public int GenericParameterPosition
  61. {
  62. get
  63. {
  64. if (_position == -1)
  65. {
  66. IGenericParameter[] parameters =
  67. DeclaringMethod == null ? DeclaringMethod.GenericInfo.GenericParameters : DeclaringType.GenericInfo.GenericParameters;
  68. _position = Array.IndexOf(parameters, this);
  69. }
  70. return _position;
  71. }
  72. }
  73. public bool MustHaveDefaultConstructor
  74. {
  75. get
  76. {
  77. // TODO
  78. return false;
  79. }
  80. }
  81. public Variance Variance
  82. {
  83. // TODO
  84. get { return Variance.Invariant; }
  85. }
  86. public IType[] GetBaseTypeConstraints()
  87. {
  88. // TODO
  89. return null;
  90. }
  91. public bool IsValueType
  92. {
  93. // TODO: reflect value-type constraint
  94. get { return false; }
  95. }
  96. public bool IsClass
  97. {
  98. // TODO: reflect reference-type constraint
  99. get { return false; }
  100. }
  101. public IType DeclaringType
  102. {
  103. get
  104. {
  105. return (IType)_declaringType.Entity;
  106. }
  107. }
  108. public IMethod DeclaringMethod
  109. {
  110. get { return DeclaringEntity as IMethod; }
  111. }
  112. public IEntity DeclaringEntity
  113. {
  114. get
  115. {
  116. return ((Node)_declaringMethod ?? (Node)_declaringType).Entity;
  117. }
  118. }
  119. bool IType.IsAbstract
  120. {
  121. get { return false; }
  122. }
  123. bool IType.IsInterface
  124. {
  125. get { return false; }
  126. }
  127. bool IType.IsEnum
  128. {
  129. get { return false; }
  130. }
  131. public bool IsByRef
  132. {
  133. get { return false; }
  134. }
  135. bool IType.IsFinal
  136. {
  137. get { return true; }
  138. }
  139. bool IType.IsArray
  140. {
  141. get { return false; }
  142. }
  143. public int GetTypeDepth()
  144. {
  145. return DeclaringType.GetTypeDepth() + 1;
  146. }
  147. IType IType.GetElementType()
  148. {
  149. return null;
  150. }
  151. public IType BaseType
  152. {
  153. // TODO: Return base constraint or system.object
  154. get { return _tss.ObjectType; }
  155. }
  156. public IEntity GetDefaultMember()
  157. {
  158. return null;
  159. }
  160. public IConstructor[] GetConstructors()
  161. {
  162. return null;
  163. }
  164. public IType[] GetInterfaces()
  165. {
  166. // TODO: return interface constraints and inherited interfaces
  167. return _emptyTypeArray;
  168. }
  169. public bool IsSubclassOf(IType other)
  170. {
  171. return (other == BaseType || BaseType.IsSubclassOf(other));
  172. }
  173. public bool IsAssignableFrom(IType other)
  174. {
  175. return (other == this);
  176. }
  177. IGenericTypeInfo IType.GenericInfo
  178. {
  179. get { return null; }
  180. }
  181. IConstructedTypeInfo IType.ConstructedInfo
  182. {
  183. get { return null; }
  184. }
  185. public string Name
  186. {
  187. get { return _declaration.Name; }
  188. }
  189. public string FullName
  190. {
  191. get
  192. {
  193. return string.Format("{0}.{1}", DeclaringEntity.FullName, Name);
  194. }
  195. }
  196. public EntityType EntityType
  197. {
  198. get { return EntityType.Type; }
  199. }
  200. public IType Type
  201. {
  202. get { return this; }
  203. }
  204. INamespace INamespace.ParentNamespace
  205. {
  206. get { return (INamespace)DeclaringEntity; }
  207. }
  208. IEntity[] INamespace.GetMembers()
  209. {
  210. return NullNamespace.EmptyEntityArray;
  211. }
  212. bool INamespace.Resolve(List targetList, string name, EntityType flags)
  213. {
  214. // Resolve using base type constraint
  215. if (BaseType != null)
  216. {
  217. if (BaseType.Resolve(targetList, name, flags))
  218. {
  219. return true;
  220. }
  221. }
  222. // Resolve using interface constraints
  223. foreach (IType type in GetInterfaces())
  224. {
  225. if (type.Resolve(targetList, name, flags))
  226. {
  227. return true;
  228. }
  229. }
  230. return false;
  231. }
  232. public Node Node
  233. {
  234. get { return _declaration; }
  235. }
  236. override public string ToString()
  237. {
  238. return FullName;
  239. }
  240. override public bool Equals(object rhs)
  241. {
  242. IGenericParameter p = rhs as IGenericParameter;
  243. if (null == p) return false;
  244. //TODO: >=0.9 : check base type constraints
  245. return Name == p.Name //FIXME: should be GenericParameterPosition but crashes on internal g. params(?!!)
  246. && Variance == p.Variance
  247. && MustHaveDefaultConstructor == p.MustHaveDefaultConstructor
  248. && IsClass == p.IsClass
  249. && IsValueType == p.IsValueType;
  250. }
  251. override public int GetHashCode()
  252. {
  253. return base.GetHashCode();
  254. }
  255. }
  256. }