2 // Copyright (c) 2004, Rodrigo B. de Oliveira (rbo@acm.org)
3 // All rights reserved.
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
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.
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.
29 namespace Boo
.Lang
.Compiler
.TypeSystem
34 public class CallableSignature
36 IParameter
[] _parameters
;
41 public CallableSignature(IMethod 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
;
76 public IParameter
[] Parameters
84 public IType ReturnType
92 public bool AcceptVarArgs
96 return _acceptVarArgs
;
100 override public int GetHashCode()
105 override public bool Equals(object other
)
107 CallableSignature rhs
= other
as CallableSignature
;
109 || _returnType
!= rhs
._returnType
110 || _acceptVarArgs
!= rhs
._acceptVarArgs
)
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
)
138 for (int i
=0; i
<lhs
.Length
; ++i
)
140 if (lhs
[i
].Type
!= rhs
[i
].Type
)
144 if (lhs
[i
].IsByRef
!= rhs
[i
].IsByRef
)
152 void InitializeHashCode()
154 _hashCode
= _acceptVarArgs
? 1 : 2;
155 foreach (IParameter parameter
in _parameters
)
157 _hashCode ^
= parameter
.Type
.GetHashCode();
159 _hashCode ^
= _returnType
.GetHashCode();