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