BOO-988: Added else block to the for and while statements, and include a suite of...
[boo.git] / src / Boo.Lang.Compiler / TypeSystem / CallableSignature.cs
blobc9d64fdc21924bebf651c85f059d32d431a4c8a2
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
29 namespace Boo.Lang.Compiler.TypeSystem
31 using System;
32 using System.Text;
34 public class CallableSignature
36 IParameter[] _parameters;
37 IType _returnType;
38 int _hashCode;
39 bool _acceptVarArgs;
41 public CallableSignature(IMethod method)
43 if (null == method)
45 throw new ArgumentNullException("method");
47 Initialize(method.GetParameters(), method.ReturnType, method.AcceptVarArgs);
50 public CallableSignature(IParameter[] parameters, IType returnType)
52 Initialize(parameters, returnType, false);
55 public CallableSignature(IParameter[] parameters, IType returnType, bool acceptVarArgs)
57 Initialize(parameters, returnType, acceptVarArgs);
60 private void Initialize(IParameter[] parameters, IType returnType, bool acceptVarArgs)
62 if (null == parameters)
64 throw new ArgumentNullException("parameters");
66 if (null == returnType)
68 throw new ArgumentNullException("returnType");
70 _parameters = parameters;
71 _returnType = returnType;
72 _acceptVarArgs = acceptVarArgs;
73 InitializeHashCode();
76 public IParameter[] Parameters
78 get
80 return _parameters;
84 public IType ReturnType
86 get
88 return _returnType;
92 public bool AcceptVarArgs
94 get
96 return _acceptVarArgs;
100 override public int GetHashCode()
102 return _hashCode;
105 override public bool Equals(object other)
107 CallableSignature rhs = other as CallableSignature;
108 if (null == rhs
109 || _returnType != rhs._returnType
110 || _acceptVarArgs != rhs._acceptVarArgs)
112 return false;
114 return AreSameParameters(_parameters, rhs._parameters);
117 override public string ToString()
119 StringBuilder buffer = new StringBuilder("callable(");
120 for (int i=0; i<_parameters.Length; ++i)
122 if (i > 0) { buffer.Append(", "); }
123 if (_parameters[i].IsByRef) buffer.Append("ref ");
124 if (_acceptVarArgs && i == _parameters.Length-1) buffer.Append('*');
125 buffer.Append(_parameters[i].Type.ToString());
127 buffer.Append(") as ");
128 buffer.Append(_returnType.ToString());
129 return buffer.ToString();
132 static public bool AreSameParameters(IParameter[] lhs, IParameter[] rhs)
134 if (lhs.Length != rhs.Length)
136 return false;
138 for (int i=0; i<lhs.Length; ++i)
140 if (lhs[i].Type != rhs[i].Type)
142 return false;
144 if (lhs[i].IsByRef != rhs[i].IsByRef)
146 return false;
149 return true;
152 void InitializeHashCode()
154 _hashCode = _acceptVarArgs ? 1 : 2;
155 foreach (IParameter parameter in _parameters)
157 _hashCode ^= parameter.Type.GetHashCode();
159 _hashCode ^= _returnType.GetHashCode();