2002-02-22 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / mcs / parameter.cs
1 //
2 // parameter.cs: Parameter definition.
3 //
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 //
6 // Licensed under the terms of the GNU GPL
7 //
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)
9 //
10 //
11 //
12 using System;
13 using System.Reflection;
14 using System.Reflection.Emit;
15 using System.Collections;
16
17 namespace Mono.CSharp {
18
19
20         /// <summary>
21         ///   Represents a single method parameter
22         /// </summary>
23         public class Parameter {
24                 [Flags]
25                 public enum Modifier : byte {
26                         NONE   = 0,
27                         REF    = 1,
28                         OUT    = 2,
29                         PARAMS = 4,
30                 }
31
32                 public readonly string   TypeName;
33                 public readonly string   Name;
34                 public readonly Modifier ModFlags;
35                 public Attributes OptAttributes;
36                 public Type ParameterType;
37                 
38                 public Parameter (string type, string name, Modifier mod, Attributes attrs)
39                 {
40                         Name = name;
41                         ModFlags = mod;
42                         TypeName = type;
43                         OptAttributes = attrs;
44                 }
45
46                 public bool Resolve (DeclSpace ds, Location l)
47                 {
48                         ParameterType = RootContext.LookupType (ds, TypeName, false, l);
49                         return ParameterType != null;
50                 }
51
52                 public Type ExternalType ()
53                 {
54                         if ((ModFlags & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0){
55                                 string n = ParameterType.FullName + "&";
56                                 Type t;
57                                 
58                                 t = Type.GetType (n);
59
60                                 //
61                                 // It is a type defined by the source code we are compiling
62                                 //
63                                 if (t == null){
64                                         ModuleBuilder mb = RootContext.ModuleBuilder;
65
66                                         t = mb.GetType (n);
67                                 }
68
69                                 return t;
70                         }
71
72                         return ParameterType;
73                 }
74                 
75                 public ParameterAttributes Attributes {
76                         get {
77                                 switch (ModFlags){
78                                 case Modifier.NONE:
79                                         return ParameterAttributes.None;
80                                 case Modifier.REF:
81                                         return ParameterAttributes.None;
82                                 case Modifier.OUT:
83                                         return ParameterAttributes.Out;
84                                 case Modifier.PARAMS:
85                                         return 0;
86                                 }
87                                 
88                                 return ParameterAttributes.None;
89                         }
90                 }
91                 
92                 /// <summary>
93                 ///   Returns the signature for this parameter evaluating it on the
94                 ///   @tc context
95                 /// </summary>
96                 public string GetSignature (DeclSpace ds, Location loc)
97                 {
98                         if (ParameterType == null){
99                                 if (!Resolve (ds, loc))
100                                         return null;
101                         }
102
103                         return ExternalType ().FullName;
104                 }
105         }
106
107         /// <summary>
108         ///   Represents the methods parameters
109         /// </summary>
110         public class Parameters {
111                 public Parameter [] FixedParameters;
112                 public readonly Parameter ArrayParameter;
113                 string signature;
114                 Type [] types;
115                 Location loc;
116                 
117                 static Parameters empty_parameters;
118                 
119                 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
120                 {
121                         FixedParameters = fixed_parameters;
122                         ArrayParameter  = array_parameter;
123                         loc = l;
124                 }
125
126                 /// <summary>
127                 ///   This is used to reuse a set of empty parameters, because they
128                 ///   are common
129                 /// </summary>
130                 public static Parameters GetEmptyReadOnlyParameters ()
131                 {
132                         if (empty_parameters == null)
133                                 empty_parameters = new Parameters (null, null, Location.Null);
134                         
135                         return empty_parameters;
136                 }
137                 
138                 public bool Empty {
139                         get {
140                                 return (FixedParameters == null) && (ArrayParameter == null);
141                         }
142                 }
143                 
144                 public void ComputeSignature (DeclSpace ds)
145                 {
146                         signature = "";
147                         if (FixedParameters != null){
148                                 for (int i = 0; i < FixedParameters.Length; i++){
149                                         Parameter par = FixedParameters [i];
150                                         
151                                         signature += par.GetSignature (ds, loc);
152                                 }
153                         }
154                         //
155                         // Note: as per the spec, the `params' arguments (ArrayParameter)
156                         // are not used in the signature computation for a method
157                         //
158                 }
159
160                 public bool VerifyArgs ()
161                 {
162                         int count;
163                         int i, j;
164
165                         if (FixedParameters == null)
166                                 return true;
167                         
168                         count = FixedParameters.Length;
169                         for (i = 0; i < count; i++){
170                                 for (j = i + 1; j < count; j++){
171                                         if (FixedParameters [i].Name != FixedParameters [j].Name)
172                                                 continue;
173                                         Report.Error (
174                                                 100, "The parameter name `" + FixedParameters [i].Name +
175                                                 "' is a duplicate");
176                                         return false;
177                                 }
178                         }
179                         return true;
180                 }
181                 
182                 /// <summary>
183                 ///    Returns the signature of the Parameters evaluated in
184                 ///    the @tc environment
185                 /// </summary>
186                 public string GetSignature (DeclSpace ds)
187                 {
188                         if (signature == null){
189                                 VerifyArgs ();
190                                 ComputeSignature (ds);
191                         }
192                         
193                         return signature;
194                 }
195                 
196                 /// <summary>
197                 ///    Returns the paramenter information based on the name
198                 /// </summary>
199                 public Parameter GetParameterByName (string name, out int idx)
200                 {
201                         idx = 0;
202
203                         if (FixedParameters == null)
204                                 return null;
205
206                         int i = 0;
207                         foreach (Parameter par in FixedParameters){
208                                 if (par.Name == name){
209                                         idx = i;
210                                         return par;
211                                 }
212                                 i++;
213                         }
214
215                         if (ArrayParameter != null)
216                                 if (name == ArrayParameter.Name){
217                                         idx = i;
218                                         return ArrayParameter;
219                                 }
220                         
221                         return null;
222                 }
223
224                 bool ComputeParameterTypes (DeclSpace ds)
225                 {
226                         int extra = (ArrayParameter != null) ? 1 : 0;
227                         int i = 0;
228                         int pc = FixedParameters.Length + extra;
229                         
230                         types = new Type [pc];
231                         
232                         if (!VerifyArgs ()){
233                                 FixedParameters = null;
234                                 return false;
235                         }
236                         
237                         foreach (Parameter p in FixedParameters){
238                                 Type t = null;
239                                 
240                                 if (p.Resolve (ds, loc))
241                                         t = p.ExternalType ();
242                                 
243                                 types [i] = t;
244                                 i++;
245                         }
246
247                         if (extra > 0){
248                                 if (ArrayParameter.Resolve (ds, loc))
249                                         types [i] = ArrayParameter.ExternalType ();
250                         }
251
252                         return true;
253                 }
254                 
255                 /// <summary>
256                 ///   Returns the argument types as an array
257                 /// </summary>
258                 static Type [] no_types = new Type [0];
259                 
260                 public Type [] GetParameterInfo (DeclSpace ds)
261                 {
262                         if (types != null)
263                                 return types;
264                         
265                         if (FixedParameters == null)
266                                 return no_types;
267
268                         if (ComputeParameterTypes (ds) == false)
269                                 return null;
270                         
271                         return types;
272                 }
273
274                 /// <summary>
275                 ///   Returns the type of a given parameter, and stores in the `is_out'
276                 ///   boolean whether this is an out or ref parameter.
277                 ///
278                 ///   Note that the returned type will not contain any dereference in this
279                 ///   case (ie, you get "int" for a ref int instead of "int&"
280                 /// </summary>
281                 public Type GetParameterInfo (DeclSpace ds, int idx, out bool is_out)
282                 {
283                         is_out = false;
284                         
285                         if (!VerifyArgs ()){
286                                 FixedParameters = null;
287                                 return null;
288                         }
289
290                         if (FixedParameters == null)
291                                 return null;
292                         
293                         if (types == null)
294                                 if (ComputeParameterTypes (ds) == false){
295                                         is_out = false;
296                                         return null;
297                                 }
298
299                         //
300                         // If this is a request for the variable lenght arg.
301                         //
302                         if (idx == FixedParameters.Length){
303                                 is_out = false;
304                                 return types [idx];
305                         } 
306
307                         //
308                         // Otherwise, it is a fixed parameter
309                         //
310                         Parameter p = FixedParameters [idx];
311                         is_out = ((p.ModFlags & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0);
312
313                         return p.ParameterType;
314                 }
315
316                 public CallingConventions GetCallingConvention ()
317                 {
318                         // For now this is the only correc thing to do
319                         return CallingConventions.Standard;
320                 }
321         }
322 }
323                 
324         
325