New tests, update.
[mono.git] / mcs / class / Mono.Cecil / Mono.Cecil / MethodReference.cs
1 //
2 // MethodReference.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@gmail.com)
6 //
7 // (C) 2005 - 2007 Jb Evain
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 namespace Mono.Cecil {
30
31         using System.Text;
32
33         public class MethodReference : MemberReference, IMethodSignature, IGenericParameterProvider {
34
35                 ParameterDefinitionCollection m_parameters;
36                 MethodReturnType m_returnType;
37
38                 bool m_hasThis;
39                 bool m_explicitThis;
40                 MethodCallingConvention m_callConv;
41                 GenericParameterCollection m_genparams;
42
43                 public virtual bool HasThis {
44                         get { return m_hasThis; }
45                         set { m_hasThis = value; }
46                 }
47
48                 public virtual bool ExplicitThis {
49                         get { return m_explicitThis; }
50                         set { m_explicitThis = value; }
51                 }
52
53                 public virtual MethodCallingConvention CallingConvention {
54                         get { return m_callConv; }
55                         set { m_callConv = value; }
56                 }
57
58                 public virtual ParameterDefinitionCollection Parameters {
59                         get {
60                                 if (m_parameters == null)
61                                         m_parameters = new ParameterDefinitionCollection (this);
62                                 return m_parameters;
63                         }
64                 }
65
66                 public GenericParameterCollection GenericParameters {
67                         get {
68                                 if (m_genparams == null)
69                                         m_genparams = new GenericParameterCollection (this);
70                                 return m_genparams;
71                         }
72                 }
73
74                 public virtual MethodReturnType ReturnType {
75                         get { return m_returnType;}
76                         set { m_returnType = value; }
77                 }
78
79                 internal MethodReference (string name, bool hasThis,
80                         bool explicitThis, MethodCallingConvention callConv) : this (name)
81                 {
82                         m_parameters = new ParameterDefinitionCollection (this);
83                         m_hasThis = hasThis;
84                         m_explicitThis = explicitThis;
85                         m_callConv = callConv;
86                 }
87
88                 internal MethodReference (string name) : base (name)
89                 {
90                         m_returnType = new MethodReturnType (null);
91                 }
92
93                 public MethodReference (string name,
94                         TypeReference declaringType, TypeReference returnType,
95                         bool hasThis, bool explicitThis, MethodCallingConvention callConv) :
96                         this (name, hasThis, explicitThis, callConv)
97                 {
98                         this.DeclaringType = declaringType;
99                         this.ReturnType.ReturnType = returnType;
100                 }
101
102                 public virtual MethodReference GetOriginalMethod ()
103                 {
104                         return this;
105                 }
106
107                 public int GetSentinel ()
108                 {
109                         for (int i = 0; i < Parameters.Count; i++)
110                                 if (Parameters [i].ParameterType is SentinelType)
111                                         return i;
112
113                         return -1;
114                 }
115
116                 public override string ToString ()
117                 {
118                         int sentinel = GetSentinel ();
119
120                         StringBuilder sb = new StringBuilder ();
121                         sb.Append (m_returnType.ReturnType.FullName);
122                         sb.Append (" ");
123                         sb.Append (base.ToString ());
124                         sb.Append ("(");
125                         for (int i = 0; i < this.Parameters.Count; i++) {
126                                 if (i > 0)
127                                         sb.Append (",");
128
129                                 if (i == sentinel)
130                                         sb.Append ("...,");
131
132                                 sb.Append (this.Parameters [i].ParameterType.FullName);
133                         }
134                         sb.Append (")");
135                         return sb.ToString ();
136                 }
137         }
138 }