[cil-strip] Upgrade to latest Mono.Cecil API
[mono.git] / mcs / tools / cil-strip / 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         internal 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 bool HasParameters {
59                         get { return (m_parameters == null) ? false : (m_parameters.Count > 0); }
60                 }
61
62                 public virtual ParameterDefinitionCollection Parameters {
63                         get {
64                                 if (m_parameters == null)
65                                         m_parameters = new ParameterDefinitionCollection (this);
66                                 return m_parameters;
67                         }
68                 }
69
70                 public bool HasGenericParameters {
71                         get { return (m_genparams == null) ? false : (m_genparams.Count > 0); }
72                 }
73
74                 public GenericParameterCollection GenericParameters {
75                         get {
76                                 if (m_genparams == null)
77                                         m_genparams = new GenericParameterCollection (this);
78                                 return m_genparams;
79                         }
80                 }
81
82                 public virtual MethodReturnType ReturnType {
83                         get { return m_returnType;}
84                         set { m_returnType = value; }
85                 }
86
87                 internal MethodReference (string name, bool hasThis,
88                         bool explicitThis, MethodCallingConvention callConv) : this (name)
89                 {
90                         m_parameters = new ParameterDefinitionCollection (this);
91                         m_hasThis = hasThis;
92                         m_explicitThis = explicitThis;
93                         m_callConv = callConv;
94                 }
95
96                 internal MethodReference (string name) : base (name)
97                 {
98                         m_returnType = new MethodReturnType (null);
99                 }
100
101                 public MethodReference (string name,
102                         TypeReference declaringType, TypeReference returnType,
103                         bool hasThis, bool explicitThis, MethodCallingConvention callConv) :
104                         this (name, hasThis, explicitThis, callConv)
105                 {
106                         this.DeclaringType = declaringType;
107                         this.ReturnType.ReturnType = returnType;
108                 }
109
110                 public virtual MethodDefinition Resolve ()
111                 {
112                         TypeReference declaringType = DeclaringType;
113                         if (declaringType == null)
114                                 return null;
115
116                         return declaringType.Module.Resolver.Resolve (this);
117                 }
118
119                 public virtual MethodReference GetOriginalMethod ()
120                 {
121                         return this;
122                 }
123
124                 public int GetSentinel ()
125                 {
126                         if (HasParameters) {
127                                 for (int i = 0; i < Parameters.Count; i++)
128                                         if (Parameters [i].ParameterType is SentinelType)
129                                                 return i;
130                         }
131                         return -1;
132                 }
133
134                 public override string ToString ()
135                 {
136                         int sentinel = GetSentinel ();
137
138                         StringBuilder sb = new StringBuilder ();
139                         sb.Append (m_returnType.ReturnType.FullName);
140                         sb.Append (" ");
141                         sb.Append (base.ToString ());
142                         sb.Append ("(");
143                         if (this.HasParameters) {
144                                 for (int i = 0; i < this.Parameters.Count; i++) {
145                                         if (i > 0)
146                                                 sb.Append (",");
147
148                                         if (i == sentinel)
149                                                 sb.Append ("...,");
150
151                                         sb.Append (this.Parameters [i].ParameterType.FullName);
152                                 }
153                         }
154                         sb.Append (")");
155                         return sb.ToString ();
156                 }
157         }
158 }