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