2002-10-20 Rafael Teixeira <rafaelteixeirabr@hotmail.com>
[mono.git] / mcs / mbas / 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                         // This is a flag which says that it's either REF or OUT.
31                         ISBYREF = 8
32                 }
33
34                 public readonly Expression TypeName;
35                 public readonly Modifier ModFlags;
36                 public Attributes OptAttributes;
37                 public readonly string Name;
38                 public Type parameter_type;
39                 
40                 public Parameter (Expression type, string name, Modifier mod, Attributes attrs)
41                 {
42                         Name = name;
43                         ModFlags = mod;
44                         TypeName = type;
45                         OptAttributes = attrs;
46                 }
47
48                 // <summary>
49                 //   Resolve is used in method definitions
50                 // </summary>
51                 public bool Resolve (DeclSpace ds, Location l)
52                 {
53                         parameter_type = ds.ResolveType (TypeName, false, l);
54
55                         if (parameter_type == TypeManager.void_type){
56                                 Report.Error (1536, l, "`void' parameter is not permitted");
57                                 return false;
58                         }
59                         
60                         return parameter_type != null;
61                 }
62
63                 public Type ExternalType (DeclSpace ds, Location l)
64                 {
65                         if ((ModFlags & Parameter.Modifier.ISBYREF) != 0){
66                                 string n = parameter_type.FullName + "&";
67
68                                 Type t = RootContext.LookupType (ds, n, false, l);
69
70                                 return t;
71                         }
72                         
73                         return parameter_type;
74                 }
75
76                 public Type ParameterType {
77                         get {
78                                 return parameter_type;
79                         }
80                 }
81                 
82                 public ParameterAttributes Attributes {
83                         get {
84                                 int flags = ((int) ModFlags) & ~((int) Parameter.Modifier.ISBYREF);
85                                 switch ((Modifier) flags) {
86                                 case Modifier.NONE:
87                                         return ParameterAttributes.None;
88                                 case Modifier.REF:
89                                         return ParameterAttributes.None;
90                                 case Modifier.OUT:
91                                         return ParameterAttributes.Out;
92                                 case Modifier.PARAMS:
93                                         return 0;
94                                 }
95                                 
96                                 return ParameterAttributes.None;
97                         }
98                 }
99                 
100                 /// <summary>
101                 ///   Returns the signature for this parameter evaluating it on the
102                 ///   @tc context
103                 /// </summary>
104                 public string GetSignature (DeclSpace ds, Location loc)
105                 {
106                         if (parameter_type == null){
107                                 if (!Resolve (ds, loc))
108                                         return null;
109                         }
110
111                         return ExternalType (ds, loc).FullName;
112                 }
113         }
114
115         /// <summary>
116         ///   Represents the methods parameters
117         /// </summary>
118         public class Parameters {
119                 public Parameter [] FixedParameters;
120                 public readonly Parameter ArrayParameter;
121                 string signature;
122                 Type [] types;
123                 Location loc;
124                 
125                 static Parameters empty_parameters;
126                 
127                 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
128                 {
129                         FixedParameters = fixed_parameters;
130                         ArrayParameter  = array_parameter;
131                         loc = l;
132                 }
133
134                 /// <summary>
135                 ///   This is used to reuse a set of empty parameters, because they
136                 ///   are common
137                 /// </summary>
138                 public static Parameters EmptyReadOnlyParameters {
139                         get {
140                                 if (empty_parameters == null)
141                                         empty_parameters = new Parameters (null, null, Location.Null);
142                         
143                                 return empty_parameters;
144                         }
145                 }
146                 
147                 public bool Empty {
148                         get {
149                                 return (FixedParameters == null) && (ArrayParameter == null);
150                         }
151                 }
152                 
153                 public void ComputeSignature (DeclSpace ds)
154                 {
155                         signature = "";
156                         if (FixedParameters != null){
157                                 for (int i = 0; i < FixedParameters.Length; i++){
158                                         Parameter par = FixedParameters [i];
159                                         
160                                         signature += par.GetSignature (ds, loc);
161                                 }
162                         }
163                         //
164                         // Note: as per the spec, the `params' arguments (ArrayParameter)
165                         // are not used in the signature computation for a method
166                         //
167                 }
168
169                 static void Error_DuplicateParameterName (string name)
170                 {
171                         Report.Error (
172                                 100, "The parameter name `" + name + "' is a duplicate");
173                 }
174                 
175                 public bool VerifyArgs ()
176                 {
177                         int count;
178                         int i, j;
179
180                         if (FixedParameters == null)
181                                 return true;
182                         
183                         count = FixedParameters.Length;
184                         string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
185                         for (i = 0; i < count; i++){
186                                 string base_name = FixedParameters [i].Name;
187                                 
188                                 for (j = i + 1; j < count; j++){
189                                         if (base_name != FixedParameters [j].Name)
190                                                 continue;
191                                         Error_DuplicateParameterName (base_name);
192                                         return false;
193                                 }
194
195                                 if (base_name == array_par_name){
196                                         Error_DuplicateParameterName (base_name);
197                                         return false;
198                                 }
199                         }
200                         return true;
201                 }
202                 
203                 /// <summary>
204                 ///    Returns the signature of the Parameters evaluated in
205                 ///    the @tc environment
206                 /// </summary>
207                 public string GetSignature (DeclSpace ds)
208                 {
209                         if (signature == null){
210                                 VerifyArgs ();
211                                 ComputeSignature (ds);
212                         }
213                         
214                         return signature;
215                 }
216                 
217                 /// <summary>
218                 ///    Returns the paramenter information based on the name
219                 /// </summary>
220                 public Parameter GetParameterByName (string name, out int idx)
221                 {
222                         idx = 0;
223                         int i = 0;
224
225                         if (FixedParameters != null){
226                                 foreach (Parameter par in FixedParameters){
227                                         if (par.Name == name){
228                                                 idx = i;
229                                                 return par;
230                                         }
231                                         i++;
232                                 }
233                         }
234
235                         if (ArrayParameter != null){
236                                 if (name == ArrayParameter.Name){
237                                         idx = i;
238                                         return ArrayParameter;
239                                 }
240                         }
241                         
242                         return null;
243                 }
244
245                 bool ComputeParameterTypes (DeclSpace ds)
246                 {
247                         int extra = (ArrayParameter != null) ? 1 : 0;
248                         int i = 0;
249                         int pc;
250
251                         if (FixedParameters == null)
252                                 pc = extra;
253                         else
254                                 pc = extra + FixedParameters.Length;
255
256                         types = new Type [pc];
257                         
258                         if (!VerifyArgs ()){
259                                 FixedParameters = null;
260                                 return false;
261                         }
262
263                         bool failed = false;
264                         if (FixedParameters != null){
265                                 foreach (Parameter p in FixedParameters){
266                                         Type t = null;
267                                         
268                                         if (p.Resolve (ds, loc))
269                                                 t = p.ExternalType (ds, loc);
270                                         else
271                                                 failed = true;
272                                         
273                                         types [i] = t;
274                                         i++;
275                                 }
276                         }
277                         
278                         if (extra > 0){
279                                 if (ArrayParameter.Resolve (ds, loc))
280                                         types [i] = ArrayParameter.ExternalType (ds, loc);
281                                 else 
282                                         failed = true;
283                         }
284
285                         if (failed){
286                                 types = null;
287                                 return false;
288                         }
289
290                         return true;
291                 }
292
293                 //
294                 // This variant is used by Delegates, because they need to
295                 // resolve/define names, instead of the plain LookupType
296                 //
297                 public bool ComputeAndDefineParameterTypes (DeclSpace ds)
298                 {
299                         int extra = (ArrayParameter != null) ? 1 : 0;
300                         int i = 0;
301                         int pc;
302
303                         if (FixedParameters == null)
304                                 pc = extra;
305                         else
306                                 pc = extra + FixedParameters.Length;
307                         
308                         types = new Type [pc];
309                         
310                         if (!VerifyArgs ()){
311                                 FixedParameters = null;
312                                 return false;
313                         }
314
315                         bool ok_flag = true;
316                         
317                         if (FixedParameters != null){
318                                 foreach (Parameter p in FixedParameters){
319                                         Type t = null;
320                                         
321                                         if (p.Resolve (ds, loc))
322                                                 t = p.ExternalType (ds, loc);
323                                         else
324                                                 ok_flag = false;
325                                         
326                                         types [i] = t;
327                                         i++;
328                                 }
329                         }
330                         
331                         if (extra > 0){
332                                 if (ArrayParameter.Resolve (ds, loc))
333                                         types [i] = ArrayParameter.ExternalType (ds, loc);
334                                 else
335                                         ok_flag = false;
336                         }
337
338                         //
339                         // invalidate the cached types
340                         //
341                         if (!ok_flag){
342                                 types = null;
343                         }
344                         
345                         return ok_flag;
346                 }
347                 
348                 /// <summary>
349                 ///   Returns the argument types as an array
350                 /// </summary>
351                 static Type [] no_types = new Type [0];
352                 
353                 public Type [] GetParameterInfo (DeclSpace ds)
354                 {
355                         if (types != null)
356                                 return types;
357                         
358                         if (FixedParameters == null && ArrayParameter == null)
359                                 return no_types;
360
361                         if (ComputeParameterTypes (ds) == false){
362                                 types = null;
363                                 return null;
364                         }
365
366                         return types;
367                 }
368
369                 /// <summary>
370                 ///   Returns the type of a given parameter, and stores in the `is_out'
371                 ///   boolean whether this is an out or ref parameter.
372                 ///
373                 ///   Note that the returned type will not contain any dereference in this
374                 ///   case (ie, you get "int" for a ref int instead of "int&"
375                 /// </summary>
376                 public Type GetParameterInfo (DeclSpace ds, int idx, out Parameter.Modifier mod)
377                 {
378                         mod = Parameter.Modifier.NONE;
379                         
380                         if (!VerifyArgs ()){
381                                 FixedParameters = null;
382                                 return null;
383                         }
384
385                         if (FixedParameters == null && ArrayParameter == null)
386                                 return null;
387                         
388                         if (types == null)
389                                 if (ComputeParameterTypes (ds) == false)
390                                         return null;
391
392                         //
393                         // If this is a request for the variable lenght arg.
394                         //
395                         int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
396                         if (idx == array_idx)
397                                 return types [idx];
398
399                         //
400                         // Otherwise, it is a fixed parameter
401                         //
402                         Parameter p = FixedParameters [idx];
403                         mod = p.ModFlags;
404
405                         if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
406                                 mod |= Parameter.Modifier.ISBYREF;
407
408                         return p.ParameterType;
409                 }
410
411                 public CallingConventions GetCallingConvention ()
412                 {
413                         // For now this is the only correc thing to do
414                         return CallingConventions.Standard;
415                 }
416         }
417 }
418                 
419         
420