add more todos
[mono.git] / mcs / bmcs / 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 (-28, 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
127         /// <summary>
128         ///   Represents a single method parameter
129         /// </summary>
130
131         //TODO: Add location member to this or base class for better error location and all methods simplification.
132         public class Parameter : ParameterBase {
133                 [Flags]
134                 public enum Modifier : byte {
135                         NONE    = 0,
136                         VAL     = 0,
137                         REF     = 1,
138                         OUT     = 2,
139                         PARAMS  = 4,
140                         // This is a flag which says that it's either REF or OUT.
141                         ISBYREF = 8,
142                         ARGLIST = 16,
143                         // This value is changed from the one in mbas source code
144                         OPTIONAL = 32
145                 }
146
147                 static string[] attribute_targets = new string [] { "param" };
148
149                 public Expression TypeName;
150                 public readonly Modifier ModFlags;
151                 public readonly string Name;
152                 GenericConstraints constraints;
153                 Type parameter_type;
154
155                 EmitContext ec;  // because ApplyAtrribute doesn't have ec
156
157                 /* These two fields are mbas specific. The corresponding 
158                 changes need to be merged from mbas */
159                 
160                 public readonly Expression ParameterInitializer;
161                 public readonly bool IsOptional;
162                 
163                 public Parameter (Expression type, string name, Modifier mod, Attributes attrs)
164                         : base (attrs)
165                 {
166                         Name = name;
167                         ModFlags = mod;
168                         TypeName = type;
169                         ParameterInitializer = null;
170                         IsOptional = false;
171                 }
172
173                 public Parameter (Expression type, string name, Modifier mod, Attributes attrs, Expression pi)
174                         : base (attrs)
175                 {
176                         Name = name;
177                         ModFlags = mod;
178                         TypeName = type;
179                         ParameterInitializer = pi;
180                         IsOptional = false;
181                 }
182
183                 public Parameter (Expression type, string name, Modifier mod, Attributes attrs, Expression pi, bool opt)
184                         : base (attrs)
185                 {
186                         Name = name;
187                         ModFlags = mod;
188                         TypeName = type;
189                         ParameterInitializer = pi;
190                         IsOptional = opt;
191                 }
192
193                 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
194                 {
195                         if (a.Type == TypeManager.in_attribute_type && Attributes == ParameterAttributes.Out) {
196                                 Report.Error (36, a.Location, "Can not use [In] attribute on out parameter");
197                                 return;
198                         }
199
200                         if (a.Type == TypeManager.param_array_type) {
201                                 Report.Error (674, a.Location, "Do not use 'System.ParamArrayAttribute'. Use the 'params' keyword instead");
202                                 return;
203                         }
204
205                         if (a.Type == TypeManager.out_attribute_type && (ModFlags & Modifier.REF) != 0 &&
206                             !OptAttributes.Contains (TypeManager.in_attribute_type, ec)) {
207                                 Report.Error (662, a.Location,
208                                         "'{0}' cannot specify only Out attribute on a ref parameter. Use both In and Out attributes, or neither", GetSignatureForError ());
209                                 return;
210                         }
211
212                         if (a.Type == TypeManager.cls_compliant_attribute_type) {
213                                 Report.Warning (3022, 1, a.Location, "CLSCompliant attribute has no meaning when applied to parameters. Try putting it on the method instead");
214                         }
215
216                         base.ApplyAttributeBuilder (a, cb);
217                 }
218
219                 // <summary>
220                 //   Resolve is used in method definitions
221                 // </summary>
222                 public bool Resolve (EmitContext ec, Location l)
223                 {
224                         TypeExpr texpr = TypeName.ResolveAsTypeTerminal (ec);
225                         this.ec = ec;
226
227                         if (texpr == null)
228                                 return false;
229
230                         TypeParameterExpr tparam = texpr as TypeParameterExpr;
231                         if (tparam != null)
232                                 constraints = tparam.TypeParameter.Constraints;
233
234                         parameter_type = texpr.Type;
235
236                         if (parameter_type.IsAbstract && parameter_type.IsSealed) {
237                                 Report.Error (721, l, "'{0}': static types cannot be used as parameters", GetSignatureForError ());
238                                 return false;
239                         }
240
241                         if (parameter_type == TypeManager.void_type){
242                                 Report.Error (1536, l, "Invalid parameter type 'void'");
243                                 return false;
244                         }
245
246                         if ((ModFlags & Parameter.Modifier.ISBYREF) != 0){
247                                 if (parameter_type == TypeManager.typed_reference_type ||
248                                     parameter_type == TypeManager.arg_iterator_type){
249                                         Report.Error (1601, l,
250                                                       "out or ref parameter can not be of type TypedReference or ArgIterator");
251                                         return false;
252                                 }
253                         }
254                         
255                         return parameter_type != null;
256                 }
257
258                 public Type ExternalType ()
259                 {
260                         if ((ModFlags & Parameter.Modifier.ISBYREF) != 0)
261                                 return TypeManager.GetReferenceType (parameter_type);
262                         
263                         return parameter_type;
264                 }
265
266                 public Type ParameterType {
267                         get {
268                                 return parameter_type;
269                         }
270                 }
271
272                 public GenericConstraints GenericConstraints {
273                         get {
274                                 return constraints;
275                         }
276                 }
277                 
278                 public ParameterAttributes Attributes {
279                         get {
280                                 int flags = ((int) ModFlags) & ~((int) Parameter.Modifier.ISBYREF);
281                                 switch ((Modifier) flags) {
282                                 case Modifier.NONE:
283                                         return ParameterAttributes.None;
284                                 case Modifier.REF:
285                                         return ParameterAttributes.None;
286                                 case Modifier.OUT:
287                                         return ParameterAttributes.Out;
288                                 case Modifier.PARAMS:
289                                         return 0;
290                                 }
291                                 
292                                 return ParameterAttributes.None;
293                         }
294                 }
295
296                 public static ParameterAttributes GetParameterAttributes (Modifier mod)
297                 {
298                         int flags = ((int) mod) & ~((int) Parameter.Modifier.ISBYREF);
299                         switch ((Modifier) flags) {
300                         case Modifier.NONE:
301                                 return ParameterAttributes.None;
302                         case Modifier.REF:
303                                 return ParameterAttributes.None;
304                         case Modifier.OUT:
305                                 return ParameterAttributes.Out;
306                         case Modifier.PARAMS:
307                                 return 0;
308                         }
309                                 
310                         return ParameterAttributes.None;
311                 }
312                 
313                 public override AttributeTargets AttributeTargets {
314                         get {
315                                 return AttributeTargets.Parameter;
316                         }
317                 }
318
319                 /// <summary>
320                 ///   Returns the signature for this parameter evaluating it on the
321                 ///   @tc context
322                 /// </summary>
323                 public string GetSignature (EmitContext ec, Location loc)
324                 {
325                         if (parameter_type == null){
326                                 if (!Resolve (ec, loc))
327                                         return null;
328                         }
329
330                         return ExternalType ().FullName;
331                 }
332
333                 public string GetSignatureForError ()
334                 {
335                         string typeName;
336                         if (parameter_type != null)
337                                 typeName = TypeManager.CSharpName (parameter_type);
338                         else if (TypeName.Type != null)
339                                 typeName = TypeManager.CSharpName (TypeName.Type);
340                         else
341                                 typeName = TypeName.ToString ();
342
343                         switch (ModFlags & unchecked (~Modifier.ISBYREF)) {
344                                 case Modifier.OUT:
345                                         return "out " + typeName;
346                                 case Modifier.PARAMS:
347                                         return "params " + typeName;
348                                 case Modifier.REF:
349                                         return "ref " + typeName;
350                         }
351                         return typeName;
352                 }
353
354                 public void DefineParameter (EmitContext ec, MethodBuilder mb, ConstructorBuilder cb, int index, Location loc)
355                 {
356                         ParameterAttributes par_attr = Attributes;
357                                         
358                         if (mb == null)
359                                 builder = cb.DefineParameter (index, par_attr, Name);
360                         else 
361                                 builder = mb.DefineParameter (index, par_attr, Name);
362                                         
363                         if (OptAttributes != null) {
364                                 OptAttributes.Emit (ec, this);
365                         }
366                 }
367
368                 public override string[] ValidAttributeTargets {
369                         get {
370                                 return attribute_targets;
371                         }
372                 }
373         }
374
375         /// <summary>
376         ///   Represents the methods parameters
377         /// </summary>
378         public class Parameters {
379                 public Parameter [] FixedParameters;
380                 public readonly Parameter ArrayParameter;
381                 public readonly bool HasArglist;
382                 string signature;
383                 Type [] types;
384                 Location loc;
385                 
386                 static Parameters empty_parameters;
387                 
388                 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
389                 {
390                         FixedParameters = fixed_parameters;
391                         ArrayParameter  = array_parameter;
392                         loc = l;
393                 }
394
395                 public Parameters (Parameter [] fixed_parameters, bool has_arglist, Location l)
396                 {
397                         FixedParameters = fixed_parameters;
398                         HasArglist = has_arglist;
399                         loc = l;
400                 }
401
402                 /// <summary>
403                 ///   This is used to reuse a set of empty parameters, because they
404                 ///   are common
405                 /// </summary>
406                 public static Parameters EmptyReadOnlyParameters {
407                         get {
408                                 if (empty_parameters == null)
409                                         empty_parameters = new Parameters (null, null, Location.Null);
410                         
411                                 return empty_parameters;
412                         }
413                 }
414                 
415                 public bool Empty {
416                         get {
417                                 return (FixedParameters == null) && (ArrayParameter == null);
418                         }
419                 }
420                 
421                 public void ComputeSignature (EmitContext ec)
422                 {
423                         signature = "";
424                         if (FixedParameters != null){
425                                 for (int i = 0; i < FixedParameters.Length; i++){
426                                         Parameter par = FixedParameters [i];
427                                         
428                                         signature += par.GetSignature (ec, loc);
429                                 }
430                         }
431                         //
432                         // Note: as per the spec, the `params' arguments (ArrayParameter)
433                         // are not used in the signature computation for a method
434                         //
435                 }
436
437                 void Error_DuplicateParameterName (string name)
438                 {
439                         Report.Error (
440                                 100, loc, "The parameter name `" + name + "' is a duplicate");
441                 }
442                 
443                 public bool VerifyArgs ()
444                 {
445                         int count;
446                         int i, j;
447
448                         if (FixedParameters == null)
449                                 return true;
450                         
451                         count = FixedParameters.Length;
452                         string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
453
454                         for (i = 0; i < count; i++){
455                                 string base_name = FixedParameters [i].Name;
456                                 for (j = i + 1; j < count; j++){
457                                         if (base_name != FixedParameters [j].Name)
458                                                 continue;
459                                         Error_DuplicateParameterName (base_name);
460                                         return false;
461                                 }
462
463                                 if (base_name == array_par_name){
464                                         Error_DuplicateParameterName (base_name);
465                                         return false;
466                                 }
467                         }
468                         return true;
469                 }
470                 
471                 /// <summary>
472                 ///    Returns the signature of the Parameters evaluated in
473                 ///    the @ec EmitContext
474                 /// </summary>
475                 public string GetSignature (EmitContext ec)
476                 {
477                         if (signature == null){
478                                 VerifyArgs ();
479                                 ComputeSignature (ec);
480                         }
481                         
482                         return signature;
483                 }
484                 
485                 /// <summary>
486                 ///    Returns the paramenter information based on the name
487                 /// </summary>
488                 public Parameter GetParameterByName (string name, out int idx)
489                 {
490                         idx = 0;
491                         int i = 0;
492
493                         if (FixedParameters != null){
494                                 foreach (Parameter par in FixedParameters){
495                                         if (par.Name == name){
496                                                 idx = i;
497                                                 return par;
498                                         }
499                                         i++;
500                                 }
501                         }
502
503                         if (ArrayParameter != null){
504                                 if (name == ArrayParameter.Name){
505                                         idx = i;
506                                         return ArrayParameter;
507                                 }
508                         }
509                         
510                         return null;
511                 }
512
513                 public Parameter GetParameterByName (string name)
514                 {
515                         int idx;
516
517                         return GetParameterByName (name, out idx);
518                 }
519                 
520                 bool ComputeParameterTypes (EmitContext ec)
521                 {
522                         int extra = (ArrayParameter != null) ? 1 : 0;
523                         int i = 0;
524                         int pc;
525
526                         if (FixedParameters == null)
527                                 pc = extra;
528                         else
529                                 pc = extra + FixedParameters.Length;
530
531                         types = new Type [pc];
532                         
533                         if (!VerifyArgs ()){
534                                 FixedParameters = null;
535                                 return false;
536                         }
537
538                         bool failed = false;
539                         if (FixedParameters != null){
540                                 foreach (Parameter p in FixedParameters){
541                                         Type t = null;
542                                         
543                                         if (p.Resolve (ec, loc))
544                                                 t = p.ExternalType ();
545                                         else
546                                                 failed = true;
547
548                                         types [i] = t;
549                                         i++;
550                                 }
551                         }
552                         
553                         if (extra > 0){
554                                 if (ArrayParameter.Resolve (ec, loc))
555                                         types [i] = ArrayParameter.ExternalType ();
556                                 else 
557                                         failed = true;
558                         }
559
560                         if (failed){
561                                 types = null;
562                                 return false;
563                         }
564
565                         return true;
566                 }
567
568                 //
569                 // This variant is used by Delegates, because they need to
570                 // resolve/define names, instead of the plain LookupType
571                 //
572                 public bool ComputeAndDefineParameterTypes (EmitContext ec)
573                 {
574                         bool old_type_resolving = ec.ResolvingTypeTree;
575                         ec.ResolvingTypeTree = true;
576                         bool retval = ComputeParameterTypes (ec);
577                         ec.ResolvingTypeTree = old_type_resolving;
578                         return retval;
579                 }
580                 
581                 /// <summary>
582                 ///   Returns the argument types as an array
583                 /// </summary>
584                 static Type [] no_types = new Type [0];
585                 
586                 public Type [] GetParameterInfo (EmitContext ec)
587                 {
588                         if (types != null)
589                                 return types;
590                         
591                         if (FixedParameters == null && ArrayParameter == null)
592                                 return no_types;
593
594                         if (ComputeParameterTypes (ec) == false){
595                                 types = null;
596                                 return null;
597                         }
598
599                         return types;
600                 }
601
602                 /// <summary>
603                 ///   Returns the type of a given parameter, and stores in the `is_out'
604                 ///   boolean whether this is an out or ref parameter.
605                 ///
606                 ///   Note that the returned type will not contain any dereference in this
607                 ///   case (ie, you get "int" for a ref int instead of "int&"
608                 /// </summary>
609                 public Type GetParameterInfo (EmitContext ec, int idx, out Parameter.Modifier mod)
610                 {
611                         mod = Parameter.Modifier.NONE;
612                         
613                         if (!VerifyArgs ()){
614                                 FixedParameters = null;
615                                 return null;
616                         }
617
618                         if (FixedParameters == null && ArrayParameter == null)
619                                 return null;
620                         
621                         if (types == null)
622                                 if (ComputeParameterTypes (ec) == false)
623                                         return null;
624
625                         //
626                         // If this is a request for the variable lenght arg.
627                         //
628                         int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
629                         if (idx == array_idx)
630                                 return types [idx];
631
632                         //
633                         // Otherwise, it is a fixed parameter
634                         //
635                         Parameter p = FixedParameters [idx];
636                         mod = p.ModFlags;
637
638                         if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
639                                 mod |= Parameter.Modifier.ISBYREF;
640
641                         return p.ParameterType;
642                 }
643
644                 public CallingConventions GetCallingConvention ()
645                 {
646                         if (HasArglist)
647                                 return CallingConventions.VarArgs;
648                         else
649                         return CallingConventions.Standard;
650                 }
651
652                 //
653                 // The method's attributes are passed in because we need to extract
654                 // the "return:" attribute from there to apply on the return type
655                 //
656                 public void LabelParameters (EmitContext ec,
657                         MethodBase builder,
658                         Location loc) {
659                         //
660                         // Define each type attribute (in/out/ref) and
661                         // the argument names.
662                         //
663                         int i = 0;
664                         
665                         MethodBuilder mb = builder as MethodBuilder;
666                         ConstructorBuilder cb = builder as ConstructorBuilder;
667
668                         if (FixedParameters != null) {
669                                 for (i = 0; i < FixedParameters.Length; i++) {
670                                         FixedParameters [i].DefineParameter (ec, mb, cb, i + 1, loc);
671                                 }
672                         }
673
674                         if (ArrayParameter != null){
675                                 ParameterBuilder pb;
676                                 Parameter array_param = ArrayParameter;
677
678                                 if (mb == null)
679                                         pb = cb.DefineParameter (
680                                                 i + 1, array_param.Attributes,
681                                                 array_param.Name);
682                                 else
683                                         pb = mb.DefineParameter (
684                                                 i + 1, array_param.Attributes,
685                                                 array_param.Name);
686                                         
687                                 CustomAttributeBuilder a = new CustomAttributeBuilder (
688                                         TypeManager.cons_param_array_attribute, new object [0]);
689                                 
690                                 pb.SetCustomAttribute (a);
691                         }
692                 }
693
694                 public Parameters Copy (Location l)
695                 {
696                         Parameters p = new Parameters (null, null, l);
697                         p.FixedParameters = new Parameter[this.FixedParameters.Length];
698                         this.FixedParameters.CopyTo (p.FixedParameters, 0);
699                 
700                         return (p);
701                 }
702
703                 public void AppendParameter (Parameter p)
704                 {
705                         if (FixedParameters != null) 
706                         {
707                                 Parameter [] pa = new Parameter [FixedParameters.Length+1];
708                                 FixedParameters.CopyTo (pa, 0);
709                                 pa[FixedParameters.Length] = p;
710                                 FixedParameters = pa;           
711                         }
712                         else
713                         {
714                                 FixedParameters = new Parameter [1];
715                                 FixedParameters[0] = p;
716                         }
717
718                 }
719         }
720 }