PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/w4x/boolangstudio
C# | 164 lines | 119 code | 20 blank | 25 comment | 23 complexity | 3d118b1807ec01fb8c4c2e7cfc3d5f13 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.Text;
  32. public class CallableSignature
  33. {
  34. IParameter[] _parameters;
  35. IType _returnType;
  36. int _hashCode;
  37. bool _acceptVarArgs;
  38. public CallableSignature(IMethod method)
  39. {
  40. if (null == method)
  41. {
  42. throw new ArgumentNullException("method");
  43. }
  44. Initialize(method.GetParameters(), method.ReturnType, method.AcceptVarArgs);
  45. }
  46. public CallableSignature(IParameter[] parameters, IType returnType)
  47. {
  48. Initialize(parameters, returnType, false);
  49. }
  50. public CallableSignature(IParameter[] parameters, IType returnType, bool acceptVarArgs)
  51. {
  52. Initialize(parameters, returnType, acceptVarArgs);
  53. }
  54. private void Initialize(IParameter[] parameters, IType returnType, bool acceptVarArgs)
  55. {
  56. if (null == parameters)
  57. {
  58. throw new ArgumentNullException("parameters");
  59. }
  60. if (null == returnType)
  61. {
  62. throw new ArgumentNullException("returnType");
  63. }
  64. _parameters = parameters;
  65. _returnType = returnType;
  66. _acceptVarArgs = acceptVarArgs;
  67. InitializeHashCode();
  68. }
  69. public IParameter[] Parameters
  70. {
  71. get
  72. {
  73. return _parameters;
  74. }
  75. }
  76. public IType ReturnType
  77. {
  78. get
  79. {
  80. return _returnType;
  81. }
  82. }
  83. public bool AcceptVarArgs
  84. {
  85. get
  86. {
  87. return _acceptVarArgs;
  88. }
  89. }
  90. override public int GetHashCode()
  91. {
  92. return _hashCode;
  93. }
  94. override public bool Equals(object other)
  95. {
  96. CallableSignature rhs = other as CallableSignature;
  97. if (null == rhs
  98. || !_returnType.Equals(rhs._returnType)
  99. || _acceptVarArgs != rhs._acceptVarArgs)
  100. return false;
  101. return AreSameParameters(_parameters, rhs._parameters);
  102. }
  103. override public string ToString()
  104. {
  105. StringBuilder buffer = new StringBuilder("callable(");
  106. for (int i=0; i<_parameters.Length; ++i)
  107. {
  108. if (i > 0) { buffer.Append(", "); }
  109. if (_parameters[i].IsByRef) buffer.Append("ref ");
  110. if (_acceptVarArgs && i == _parameters.Length-1) buffer.Append('*');
  111. buffer.Append(_parameters[i].Type.ToString());
  112. }
  113. buffer.Append(") as ");
  114. buffer.Append(_returnType.ToString());
  115. return buffer.ToString();
  116. }
  117. static public bool AreSameParameters(IParameter[] lhs, IParameter[] rhs)
  118. {
  119. int len = lhs.Length;
  120. if (len != rhs.Length)
  121. return false;
  122. for (int i=0; i < len; ++i)
  123. {
  124. IParameter lp = lhs[i];
  125. IParameter rp = rhs[i];
  126. if (lp.IsByRef != rp.IsByRef)
  127. return false;
  128. IType rpType = rp.IsByRef ? rp.Type.GetElementType() : rp.Type;
  129. if (!lp.Type.Equals(rpType))
  130. return false;
  131. }
  132. return true;
  133. }
  134. void InitializeHashCode()
  135. {
  136. _hashCode = _acceptVarArgs ? 1 : 2;
  137. foreach (IParameter parameter in _parameters)
  138. {
  139. _hashCode ^= parameter.Type.GetHashCode();
  140. }
  141. _hashCode ^= _returnType.GetHashCode();
  142. }
  143. }
  144. }