2004-05-24 Martin Baulig <martin@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         /// <summary>
20         ///   Abstract Base class for parameters of a method.
21         /// </summary>
22         public abstract class ParameterBase : Attributable {
23
24                 protected ParameterBuilder builder;
25
26                 public ParameterBase (Attributes attrs)
27                         : base (attrs)
28                 {
29                 }
30
31                 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
32                 {
33                         if (a.Type == TypeManager.marshal_as_attr_type) {
34                                 UnmanagedMarshal marshal = a.GetMarshal ();
35                                 if (marshal != null) {
36                                         builder.SetMarshal (marshal);
37                                         return;
38                                 }
39                                 Report.Warning_T (-24, a.Location);
40                                 return;
41                         }
42
43                         builder.SetCustomAttribute (cb);
44                 }
45
46                 public override bool IsClsCompliaceRequired(DeclSpace ds)
47                 {
48                         return false;
49                 }
50         }
51
52         /// <summary>
53         /// Class for applying custom attributes on the return type
54         /// </summary>
55         public class ReturnParameter: ParameterBase {
56                 public ReturnParameter (MethodBuilder mb, Location location):
57                         base (null)
58                 {
59                         try {
60                                 builder = mb.DefineParameter (0, ParameterAttributes.None, "");                 
61                         }
62                         catch (ArgumentOutOfRangeException) {
63                                 Report.Warning_T (-28, location);
64                         }
65                 }
66
67                 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
68                 {
69                         // This occurs after Warning -28
70                         if (builder == null)
71                                 return;
72
73                         base.ApplyAttributeBuilder (a, cb);
74                 }
75
76                 public override AttributeTargets AttributeTargets {
77                         get {
78                                 return AttributeTargets.ReturnValue;
79                         }
80                 }
81
82                 /// <summary>
83                 /// Is never called
84                 /// </summary>
85                 protected override string[] ValidAttributeTargets {
86                         get {
87                                 return null;
88                         }
89                 }
90         }
91
92         /// <summary>
93         /// Class for applying custom attributes on the implicit parameter type
94         /// of the 'set' method in properties, and the 'add' and 'remove' methods in events.
95         /// </summary>
96         public class ImplicitParameter: ParameterBase {
97                 public ImplicitParameter (MethodBuilder mb):
98                         base (null)
99                 {
100                         builder = mb.DefineParameter (1, ParameterAttributes.None, "");                 
101                 }
102
103                 public override AttributeTargets AttributeTargets {
104                         get {
105                                 return AttributeTargets.Parameter;
106                         }
107                 }
108
109                 /// <summary>
110                 /// Is never called
111                 /// </summary>
112                 protected override string[] ValidAttributeTargets {
113                         get {
114                                 return null;
115                         }
116                 }
117         }
118
119
120         /// <summary>
121         ///   Represents a single method parameter
122         /// </summary>
123
124         //TODO: Add location member to this or base class for better error location and all methods simplification.
125         public class Parameter : ParameterBase {
126                 [Flags]
127                 public enum Modifier : byte {
128                         NONE    = 0,
129                         REF     = 1,
130                         OUT     = 2,
131                         PARAMS  = 4,
132                         // This is a flag which says that it's either REF or OUT.
133                         ISBYREF = 8
134                 }
135
136                 static string[] attribute_targets = new string [] { "param" };
137
138                 public readonly Expression TypeName;
139                 public readonly Modifier ModFlags;
140                 public readonly string Name;
141                 Type parameter_type;
142                 
143                 public Parameter (Expression type, string name, Modifier mod, Attributes attrs)
144                         : base (attrs)
145                 {
146                         Name = name;
147                         ModFlags = mod;
148                         TypeName = type;
149                 }
150
151                 // <summary>
152                 //   Resolve is used in method definitions
153                 // </summary>
154                 public bool Resolve (DeclSpace ds, Location l)
155                 {
156                         parameter_type = ds.ResolveType (TypeName, false, l);
157
158                         if (parameter_type == TypeManager.void_type){
159                                 Report.Error (1536, l, "`void' parameter is not permitted");
160                                 return false;
161                         }
162
163                         if ((ModFlags & Parameter.Modifier.ISBYREF) != 0){
164                                 if (parameter_type == TypeManager.typed_reference_type ||
165                                     parameter_type == TypeManager.arg_iterator_type){
166                                         Report.Error (1601, l,
167                                                       "out or ref parameter can not be of type TypedReference or ArgIterator");
168                                         return false;
169                                 }
170                         }
171                         
172                         return parameter_type != null;
173                 }
174
175                 public Type ExternalType ()
176                 {
177                         if ((ModFlags & Parameter.Modifier.ISBYREF) != 0)
178                                 return TypeManager.GetReferenceType (parameter_type);
179                         
180                         return parameter_type;
181                 }
182
183                 public Type ParameterType {
184                         get {
185                                 return parameter_type;
186                         }
187                 }
188                 
189                 public ParameterAttributes Attributes {
190                         get {
191                                 int flags = ((int) ModFlags) & ~((int) Parameter.Modifier.ISBYREF);
192                                 switch ((Modifier) flags) {
193                                 case Modifier.NONE:
194                                         return ParameterAttributes.None;
195                                 case Modifier.REF:
196                                         return ParameterAttributes.None;
197                                 case Modifier.OUT:
198                                         return ParameterAttributes.Out;
199                                 case Modifier.PARAMS:
200                                         return 0;
201                                 }
202                                 
203                                 return ParameterAttributes.None;
204                         }
205                 }
206                 
207                 public override AttributeTargets AttributeTargets {
208                         get {
209                                 return AttributeTargets.Parameter;
210                         }
211                 }
212
213                 /// <summary>
214                 ///   Returns the signature for this parameter evaluating it on the
215                 ///   @tc context
216                 /// </summary>
217                 public string GetSignature (DeclSpace ds, Location loc)
218                 {
219                         if (parameter_type == null){
220                                 if (!Resolve (ds, loc))
221                                         return null;
222                         }
223
224                         return ExternalType ().FullName;
225                 }
226
227                 public string GetSignatureForError ()
228                 {
229                         string typeName = TypeManager.CSharpName (parameter_type);
230                         switch (ModFlags & ~Modifier.ISBYREF) {
231                                 case Modifier.OUT:
232                                         return "out " + typeName;
233                                 case Modifier.PARAMS:
234                                         return "params " + typeName;
235                                 case Modifier.REF:
236                                         return "ref " + typeName;
237                         }
238                         return typeName;
239                 }
240
241                 public void DefineParameter (EmitContext ec, MethodBuilder mb, ConstructorBuilder cb, int index, Location loc)
242                 {
243                         ParameterAttributes par_attr = Attributes;
244                                         
245                         if (mb == null)
246                                 builder = cb.DefineParameter (index, par_attr, Name);
247                         else 
248                                 builder = mb.DefineParameter (index, par_attr, Name);
249                                         
250                         if (OptAttributes != null) {
251                                 OptAttributes.Emit (ec, this);
252         
253                                 if (par_attr == ParameterAttributes.Out){
254                                         if (OptAttributes.Contains (TypeManager.in_attribute_type, ec))
255                                                 Report.Error (36, loc,  "Can not use [In] attribute on out parameter");
256                                 }
257                         }
258                 }
259
260                 protected override string[] ValidAttributeTargets {
261                         get {
262                                 return attribute_targets;
263                         }
264                 }
265         }
266
267         /// <summary>
268         ///   Represents the methods parameters
269         /// </summary>
270         public class Parameters {
271                 public Parameter [] FixedParameters;
272                 public readonly Parameter ArrayParameter;
273                 string signature;
274                 Type [] types;
275                 Location loc;
276                 
277                 static Parameters empty_parameters;
278                 
279                 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
280                 {
281                         FixedParameters = fixed_parameters;
282                         ArrayParameter  = array_parameter;
283                         loc = l;
284                 }
285
286                 /// <summary>
287                 ///   This is used to reuse a set of empty parameters, because they
288                 ///   are common
289                 /// </summary>
290                 public static Parameters EmptyReadOnlyParameters {
291                         get {
292                                 if (empty_parameters == null)
293                                         empty_parameters = new Parameters (null, null, Location.Null);
294                         
295                                 return empty_parameters;
296                         }
297                 }
298                 
299                 public bool Empty {
300                         get {
301                                 return (FixedParameters == null) && (ArrayParameter == null);
302                         }
303                 }
304                 
305                 public void ComputeSignature (DeclSpace ds)
306                 {
307                         signature = "";
308                         if (FixedParameters != null){
309                                 for (int i = 0; i < FixedParameters.Length; i++){
310                                         Parameter par = FixedParameters [i];
311                                         
312                                         signature += par.GetSignature (ds, loc);
313                                 }
314                         }
315                         //
316                         // Note: as per the spec, the `params' arguments (ArrayParameter)
317                         // are not used in the signature computation for a method
318                         //
319                 }
320
321                 void Error_DuplicateParameterName (string name)
322                 {
323                         Report.Error (
324                                 100, loc, "The parameter name `" + name + "' is a duplicate");
325                 }
326                 
327                 public bool VerifyArgs ()
328                 {
329                         int count;
330                         int i, j;
331
332                         if (FixedParameters == null)
333                                 return true;
334                         
335                         count = FixedParameters.Length;
336                         string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
337                         for (i = 0; i < count; i++){
338                                 string base_name = FixedParameters [i].Name;
339                                 
340                                 for (j = i + 1; j < count; j++){
341                                         if (base_name != FixedParameters [j].Name)
342                                                 continue;
343                                         Error_DuplicateParameterName (base_name);
344                                         return false;
345                                 }
346
347                                 if (base_name == array_par_name){
348                                         Error_DuplicateParameterName (base_name);
349                                         return false;
350                                 }
351                         }
352                         return true;
353                 }
354                 
355                 /// <summary>
356                 ///    Returns the signature of the Parameters evaluated in
357                 ///    the @tc environment
358                 /// </summary>
359                 public string GetSignature (DeclSpace ds)
360                 {
361                         if (signature == null){
362                                 VerifyArgs ();
363                                 ComputeSignature (ds);
364                         }
365                         
366                         return signature;
367                 }
368                 
369                 /// <summary>
370                 ///    Returns the paramenter information based on the name
371                 /// </summary>
372                 public Parameter GetParameterByName (string name, out int idx)
373                 {
374                         idx = 0;
375                         int i = 0;
376
377                         if (FixedParameters != null){
378                                 foreach (Parameter par in FixedParameters){
379                                         if (par.Name == name){
380                                                 idx = i;
381                                                 return par;
382                                         }
383                                         i++;
384                                 }
385                         }
386
387                         if (ArrayParameter != null){
388                                 if (name == ArrayParameter.Name){
389                                         idx = i;
390                                         return ArrayParameter;
391                                 }
392                         }
393                         
394                         return null;
395                 }
396
397                 bool ComputeParameterTypes (DeclSpace ds)
398                 {
399                         int extra = (ArrayParameter != null) ? 1 : 0;
400                         int i = 0;
401                         int pc;
402
403                         if (FixedParameters == null)
404                                 pc = extra;
405                         else
406                                 pc = extra + FixedParameters.Length;
407
408                         types = new Type [pc];
409                         
410                         if (!VerifyArgs ()){
411                                 FixedParameters = null;
412                                 return false;
413                         }
414
415                         bool failed = false;
416                         if (FixedParameters != null){
417                                 foreach (Parameter p in FixedParameters){
418                                         Type t = null;
419                                         
420                                         if (p.Resolve (ds, loc))
421                                                 t = p.ExternalType ();
422                                         else
423                                                 failed = true;
424
425                                         types [i] = t;
426                                         i++;
427                                 }
428                         }
429                         
430                         if (extra > 0){
431                                 if (ArrayParameter.Resolve (ds, loc))
432                                         types [i] = ArrayParameter.ExternalType ();
433                                 else 
434                                         failed = true;
435                         }
436
437                         if (failed){
438                                 types = null;
439                                 return false;
440                         }
441
442                         return true;
443                 }
444
445                 //
446                 // This variant is used by Delegates, because they need to
447                 // resolve/define names, instead of the plain LookupType
448                 //
449                 public bool ComputeAndDefineParameterTypes (DeclSpace ds)
450                 {
451                         int extra = (ArrayParameter != null) ? 1 : 0;
452                         int i = 0;
453                         int pc;
454
455                         if (FixedParameters == null)
456                                 pc = extra;
457                         else
458                                 pc = extra + FixedParameters.Length;
459                         
460                         types = new Type [pc];
461                         
462                         if (!VerifyArgs ()){
463                                 FixedParameters = null;
464                                 return false;
465                         }
466
467                         bool ok_flag = true;
468                         
469                         if (FixedParameters != null){
470                                 foreach (Parameter p in FixedParameters){
471                                         Type t = null;
472                                         
473                                         if (p.Resolve (ds, loc))
474                                                 t = p.ExternalType ();
475                                         else
476                                                 ok_flag = false;
477                                         
478                                         types [i] = t;
479                                         i++;
480                                 }
481                         }
482                         
483                         if (extra > 0){
484                                 if (ArrayParameter.Resolve (ds, loc))
485                                         types [i] = ArrayParameter.ExternalType ();
486                                 else
487                                         ok_flag = false;
488                         }
489
490                         //
491                         // invalidate the cached types
492                         //
493                         if (!ok_flag){
494                                 types = null;
495                         }
496                         
497                         return ok_flag;
498                 }
499                 
500                 /// <summary>
501                 ///   Returns the argument types as an array
502                 /// </summary>
503                 static Type [] no_types = new Type [0];
504                 
505                 public Type [] GetParameterInfo (DeclSpace ds)
506                 {
507                         if (types != null)
508                                 return types;
509                         
510                         if (FixedParameters == null && ArrayParameter == null)
511                                 return no_types;
512
513                         if (ComputeParameterTypes (ds) == false){
514                                 types = null;
515                                 return null;
516                         }
517
518                         return types;
519                 }
520
521                 /// <summary>
522                 ///   Returns the type of a given parameter, and stores in the `is_out'
523                 ///   boolean whether this is an out or ref parameter.
524                 ///
525                 ///   Note that the returned type will not contain any dereference in this
526                 ///   case (ie, you get "int" for a ref int instead of "int&"
527                 /// </summary>
528                 public Type GetParameterInfo (DeclSpace ds, int idx, out Parameter.Modifier mod)
529                 {
530                         mod = Parameter.Modifier.NONE;
531                         
532                         if (!VerifyArgs ()){
533                                 FixedParameters = null;
534                                 return null;
535                         }
536
537                         if (FixedParameters == null && ArrayParameter == null)
538                                 return null;
539                         
540                         if (types == null)
541                                 if (ComputeParameterTypes (ds) == false)
542                                         return null;
543
544                         //
545                         // If this is a request for the variable lenght arg.
546                         //
547                         int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
548                         if (idx == array_idx)
549                                 return types [idx];
550
551                         //
552                         // Otherwise, it is a fixed parameter
553                         //
554                         Parameter p = FixedParameters [idx];
555                         mod = p.ModFlags;
556
557                         if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
558                                 mod |= Parameter.Modifier.ISBYREF;
559
560                         return p.ParameterType;
561                 }
562
563                 public CallingConventions GetCallingConvention ()
564                 {
565                         // For now this is the only correc thing to do
566                         return CallingConventions.Standard;
567                 }
568
569                 //
570                 // The method's attributes are passed in because we need to extract
571                 // the "return:" attribute from there to apply on the return type
572                 //
573                 public void LabelParameters (EmitContext ec,
574                         MethodBase builder,
575                         Location loc) {
576                         //
577                         // Define each type attribute (in/out/ref) and
578                         // the argument names.
579                         //
580                         int i = 0;
581                         
582                         MethodBuilder mb = builder as MethodBuilder;
583                         ConstructorBuilder cb = builder as ConstructorBuilder;
584
585                         if (FixedParameters != null) {
586                                 for (i = 0; i < FixedParameters.Length; i++) {
587                                         FixedParameters [i].DefineParameter (ec, mb, cb, i + 1, loc);
588                                 }
589                         }
590
591                         if (ArrayParameter != null){
592                                 ParameterBuilder pb;
593                                 Parameter array_param = ArrayParameter;
594
595                                 if (mb == null)
596                                         pb = cb.DefineParameter (
597                                                 i + 1, array_param.Attributes,
598                                                 array_param.Name);
599                                 else
600                                         pb = mb.DefineParameter (
601                                                 i + 1, array_param.Attributes,
602                                                 array_param.Name);
603                                         
604                                 CustomAttributeBuilder a = new CustomAttributeBuilder (
605                                         TypeManager.cons_param_array_attribute, new object [0]);
606                                 
607                                 pb.SetCustomAttribute (a);
608                         }
609                 }
610         }
611 }