Add new BMCS: based on Jambu's code but migrated to 'gmcs' so we get generics support
[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                         // This occurs after Warning -28
73                         if (builder == null)
74                                 return;
75
76                         base.ApplyAttributeBuilder (a, cb);
77                 }
78
79                 public override AttributeTargets AttributeTargets {
80                         get {
81                                 return AttributeTargets.ReturnValue;
82                         }
83                 }
84
85                 /// <summary>
86                 /// Is never called
87                 /// </summary>
88                 public override string[] ValidAttributeTargets {
89                         get {
90                                 return null;
91                         }
92                 }
93         }
94
95         /// <summary>
96        /// Class for applying custom attributes on the implicit parameter type
97        /// of the 'set' method in properties, and the 'add' and 'remove' methods in events.
98         /// </summary>
99        public class ImplicitParameter: ParameterBase {
100                public ImplicitParameter (MethodBuilder mb):
101                         base (null)
102                 {
103                         builder = mb.DefineParameter (1, ParameterAttributes.None, "");                 
104                 }
105
106                 public override AttributeTargets AttributeTargets {
107                         get {
108                                 return AttributeTargets.Parameter;
109                         }
110                 }
111
112                 /// <summary>
113                 /// Is never called
114                 /// </summary>
115                 public override string[] ValidAttributeTargets {
116                         get {
117                                 return null;
118                         }
119         }
120         }
121
122
123         /// <summary>
124         ///   Represents a single method parameter
125         /// </summary>
126
127         //TODO: Add location member to this or base class for better error location and all methods simplification.
128         public class Parameter : ParameterBase {
129                 [Flags]
130                 public enum Modifier : byte {
131                         NONE    = 0,
132                         VAL     = 0,
133                         REF     = 1,
134                         OUT     = 2,
135                         PARAMS  = 4,
136                         // This is a flag which says that it's either REF or OUT.
137                         ISBYREF = 8,
138                         ARGLIST = 16,
139                         // This value is changed from the one in mbas source code
140                         OPTIONAL = 32
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                 GenericConstraints constraints;
149                 Type parameter_type;
150
151                 /* These two fields are mbas specific. The corresponding 
152                 changes need to be merged from mbas */
153                 
154                 public readonly Expression ParameterInitializer;
155                 public readonly bool IsOptional;
156                 
157                 public Parameter (Expression type, string name, Modifier mod, Attributes attrs)
158                         : base (attrs)
159                 {
160                         Name = name;
161                         ModFlags = mod;
162                         TypeName = type;
163                         ParameterInitializer = null;
164                         IsOptional = false;
165                 }
166
167                 public Parameter (Expression type, string name, Modifier mod, Attributes attrs, Expression pi)
168                         : base (attrs)
169                 {
170                         Name = name;
171                         ModFlags = mod;
172                         TypeName = type;
173                         ParameterInitializer = pi;
174                         IsOptional = false;
175                 }
176
177                 public Parameter (Expression type, string name, Modifier mod, Attributes attrs, Expression pi, bool opt)
178                         : base (attrs)
179                 {
180                         Name = name;
181                         ModFlags = mod;
182                         TypeName = type;
183                         ParameterInitializer = pi;
184                         IsOptional = opt;
185                 }
186
187                 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
188                 {
189                         if (a.Type == TypeManager.param_array_type) {
190                                 Report.Error (674, a.Location, "Do not use 'System.ParamArrayAttribute'. Use the 'params' keyword instead");
191                                 return;
192                         }
193                         base.ApplyAttributeBuilder (a, cb);
194                 }
195
196                 // <summary>
197                 //   Resolve is used in method definitions
198                 // </summary>
199                 public bool Resolve (EmitContext ec, Location l)
200                 {
201                         TypeExpr texpr = TypeName.ResolveAsTypeTerminal (ec);
202                         if (texpr == null)
203                                 return false;
204
205                         TypeParameterExpr tparam = texpr as TypeParameterExpr;
206                         if (tparam != null)
207                                 constraints = tparam.TypeParameter.Constraints;
208
209                         parameter_type = texpr.Type;
210
211                         if (parameter_type.IsAbstract && parameter_type.IsSealed) {
212                                 Report.Error (721, l, "'{0}': static types cannot be used as parameters", GetSignatureForError ());
213                                 return false;
214                         }
215
216                         if (parameter_type == TypeManager.void_type){
217                                 Report.Error (1536, l, "`void' parameter is not permitted");
218                                 return false;
219                         }
220
221                         if ((ModFlags & Parameter.Modifier.ISBYREF) != 0){
222                                 if (parameter_type == TypeManager.typed_reference_type ||
223                                     parameter_type == TypeManager.arg_iterator_type){
224                                         Report.Error (1601, l,
225                                                       "out or ref parameter can not be of type TypedReference or ArgIterator");
226                                         return false;
227                                 }
228                         }
229                         
230                         return parameter_type != null;
231                 }
232
233                 public Type ExternalType ()
234                 {
235                         if ((ModFlags & Parameter.Modifier.ISBYREF) != 0)
236                                 return TypeManager.GetReferenceType (parameter_type);
237                         
238                         return parameter_type;
239                 }
240
241                 public Type ParameterType {
242                         get {
243                                 return parameter_type;
244                         }
245                 }
246
247                 public GenericConstraints GenericConstraints {
248                         get {
249                                 return constraints;
250                         }
251                 }
252                 
253                 public ParameterAttributes Attributes {
254                         get {
255                                 int flags = ((int) ModFlags) & ~((int) Parameter.Modifier.ISBYREF);
256                                 switch ((Modifier) flags) {
257                                 case Modifier.NONE:
258                                         return ParameterAttributes.None;
259                                 case Modifier.REF:
260                                         return ParameterAttributes.None;
261                                 case Modifier.OUT:
262                                         return ParameterAttributes.Out;
263                                 case Modifier.PARAMS:
264                                         return 0;
265                                 }
266                                 
267                                 return ParameterAttributes.None;
268                         }
269                 }
270
271                 public static ParameterAttributes GetParameterAttributes (Modifier mod)
272                 {
273                         int flags = ((int) mod) & ~((int) Parameter.Modifier.ISBYREF);
274                         switch ((Modifier) flags) {
275                         case Modifier.NONE:
276                                 return ParameterAttributes.None;
277                         case Modifier.REF:
278                                 return ParameterAttributes.None;
279                         case Modifier.OUT:
280                                 return ParameterAttributes.Out;
281                         case Modifier.PARAMS:
282                                 return 0;
283                         }
284                                 
285                         return ParameterAttributes.None;
286                 }
287                 
288                 public override AttributeTargets AttributeTargets {
289                         get {
290                                 return AttributeTargets.Parameter;
291                         }
292                 }
293
294                 /// <summary>
295                 ///   Returns the signature for this parameter evaluating it on the
296                 ///   @tc context
297                 /// </summary>
298                 public string GetSignature (EmitContext ec, Location loc)
299                 {
300                         if (parameter_type == null){
301                                 if (!Resolve (ec, loc))
302                                         return null;
303                         }
304
305                         return ExternalType ().FullName;
306                 }
307
308                 public string GetSignatureForError ()
309                 {
310                         string typeName;
311                         if (parameter_type != null)
312                                 typeName = TypeManager.CSharpName (parameter_type);
313                         else if (TypeName.Type != null)
314                                 typeName = TypeManager.CSharpName (TypeName.Type);
315                         else
316                                 typeName = TypeName.ToString ();
317
318                         switch (ModFlags & unchecked (~Modifier.ISBYREF)) {
319                                 case Modifier.OUT:
320                                         return "out " + typeName;
321                                 case Modifier.PARAMS:
322                                         return "params " + typeName;
323                                 case Modifier.REF:
324                                         return "ref " + typeName;
325                         }
326                         return typeName;
327                 }
328
329                 public void DefineParameter (EmitContext ec, MethodBuilder mb, ConstructorBuilder cb, int index, Location loc)
330                 {
331                         ParameterAttributes par_attr = Attributes;
332                                         
333                         if (mb == null)
334                                 builder = cb.DefineParameter (index, par_attr, Name);
335                         else 
336                                 builder = mb.DefineParameter (index, par_attr, Name);
337                                         
338                         if (OptAttributes != null) {
339                                 OptAttributes.Emit (ec, this);
340         
341                                 if (par_attr == ParameterAttributes.Out){
342                                         if (OptAttributes.Contains (TypeManager.in_attribute_type, ec))
343                                                 Report.Error (36, loc,  "Can not use [In] attribute on out parameter");
344                                 }
345                         }
346                 }
347
348                 public override string[] ValidAttributeTargets {
349                         get {
350                                 return attribute_targets;
351                         }
352                 }
353                 
354         }
355
356         /// <summary>
357         ///   Represents the methods parameters
358         /// </summary>
359         public class Parameters {
360                 public Parameter [] FixedParameters;
361                 public readonly Parameter ArrayParameter;
362                 public readonly bool HasArglist;
363                 string signature;
364                 Type [] types;
365                 Location loc;
366                 
367                 static Parameters empty_parameters;
368                 
369                 public Parameters (Parameter [] fixed_parameters, Parameter array_parameter, Location l)
370                 {
371                         FixedParameters = fixed_parameters;
372                         ArrayParameter  = array_parameter;
373                         loc = l;
374                 }
375
376                 public Parameters (Parameter [] fixed_parameters, bool has_arglist, Location l)
377                 {
378                         FixedParameters = fixed_parameters;
379                         HasArglist = has_arglist;
380                         loc = l;
381                 }
382
383                 /// <summary>
384                 ///   This is used to reuse a set of empty parameters, because they
385                 ///   are common
386                 /// </summary>
387                 public static Parameters EmptyReadOnlyParameters {
388                         get {
389                                 if (empty_parameters == null)
390                                         empty_parameters = new Parameters (null, null, Location.Null);
391                         
392                                 return empty_parameters;
393                         }
394                 }
395                 
396                 public bool Empty {
397                         get {
398                                 return (FixedParameters == null) && (ArrayParameter == null);
399                         }
400                 }
401                 
402                 public void ComputeSignature (EmitContext ec)
403                 {
404                         signature = "";
405                         if (FixedParameters != null){
406                                 for (int i = 0; i < FixedParameters.Length; i++){
407                                         Parameter par = FixedParameters [i];
408                                         
409                                         signature += par.GetSignature (ec, loc);
410                                 }
411                         }
412                         //
413                         // Note: as per the spec, the `params' arguments (ArrayParameter)
414                         // are not used in the signature computation for a method
415                         //
416
417                 }
418
419                 void Error_DuplicateParameterName (string name)
420                 {
421                         Report.Error (
422                                 100, loc, "The parameter name `" + name + "' is a duplicate");
423                 }
424                 
425                 public bool VerifyArgs ()
426                 {
427                         int count;
428                         int i, j;
429
430                         if (FixedParameters == null)
431                                 return true;
432                         
433                         count = FixedParameters.Length;
434                         string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
435
436                         for (i = 0; i < count; i++){
437                                 string base_name = FixedParameters [i].Name;
438                                 for (j = i + 1; j < count; j++){
439                                         if (base_name != FixedParameters [j].Name)
440                                                 continue;
441                                         Error_DuplicateParameterName (base_name);
442                                         return false;
443                                 }
444
445                                 if (base_name == array_par_name){
446                                         Error_DuplicateParameterName (base_name);
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, loc))
526                                                 t = p.ExternalType ();
527                                         else
528                                                 failed = true;
529
530                                         types [i] = t;
531                                         i++;
532
533                                 }
534                         }
535                         
536                         if (extra > 0){
537                                 if (ArrayParameter.Resolve (ec, loc))
538                                         types [i] = ArrayParameter.ExternalType ();
539                                 else 
540                                         failed = true;
541                         }
542
543                         if (failed){
544                                 types = null;
545                                 return false;
546                         }
547
548                         return true;
549                 }
550
551                 //
552                 // This variant is used by Delegates, because they need to
553                 // resolve/define names, instead of the plain LookupType
554                 //
555                 public bool ComputeAndDefineParameterTypes (EmitContext ec)
556                 {
557                         bool old_type_resolving = ec.ResolvingTypeTree;
558                         ec.ResolvingTypeTree = true;
559                         bool retval = ComputeParameterTypes (ec);
560                         ec.ResolvingTypeTree = old_type_resolving;
561                         return retval;
562                 }
563                 
564                 /// <summary>
565                 ///   Returns the argument types as an array
566                 /// </summary>
567                 static Type [] no_types = new Type [0];
568                 
569                 public Type [] GetParameterInfo (EmitContext ec)
570                 {
571                         if (types != null)
572                                 return types;
573                         
574                         if (FixedParameters == null && ArrayParameter == null)
575                                 return no_types;
576
577                         if (ComputeParameterTypes (ec) == false){
578                                 types = null;
579                                 return null;
580                         }
581
582                         return types;
583                 }
584
585                 /// <summary>
586                 ///   Returns the type of a given parameter, and stores in the `is_out'
587                 ///   boolean whether this is an out or ref parameter.
588                 ///
589                 ///   Note that the returned type will not contain any dereference in this
590                 ///   case (ie, you get "int" for a ref int instead of "int&"
591                 /// </summary>
592                 public Type GetParameterInfo (EmitContext ec, int idx, out Parameter.Modifier mod)
593                 {
594                         mod = Parameter.Modifier.NONE;
595                         
596                         if (!VerifyArgs ()){
597                                 FixedParameters = null;
598                                 return null;
599                         }
600
601                         if (FixedParameters == null && ArrayParameter == null)
602                                 return null;
603                         
604                         if (types == null)
605                                 if (ComputeParameterTypes (ec) == false)
606                                         return null;
607
608                         //
609                         // If this is a request for the variable lenght arg.
610                         //
611                         int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
612                         if (idx == array_idx)
613                                 return types [idx];
614
615                         //
616                         // Otherwise, it is a fixed parameter
617                         //
618                         Parameter p = FixedParameters [idx];
619                         mod = p.ModFlags;
620
621                         if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
622                                 mod |= Parameter.Modifier.ISBYREF;
623
624                         return p.ParameterType;
625                 }
626
627                 public CallingConventions GetCallingConvention ()
628                 {
629                         if (HasArglist)
630                                 return CallingConventions.VarArgs;
631                         else
632                         return CallingConventions.Standard;
633                 }
634
635                 //
636                 // The method's attributes are passed in because we need to extract
637                 // the "return:" attribute from there to apply on the return type
638                 //
639                 public void LabelParameters (EmitContext ec,
640                         MethodBase builder,
641                         Location loc) {
642                         //
643                         // Define each type attribute (in/out/ref) and
644                         // the argument names.
645                         //
646                         int i = 0;
647                         
648                         MethodBuilder mb = builder as MethodBuilder;
649                         ConstructorBuilder cb = builder as ConstructorBuilder;
650
651                         if (FixedParameters != null) {
652                                 for (i = 0; i < FixedParameters.Length; i++) {
653                                         FixedParameters [i].DefineParameter (ec, mb, cb, i + 1, loc);
654                                 }
655                         }
656
657                         if (ArrayParameter != null){
658                                 ParameterBuilder pb;
659                                 Parameter array_param = ArrayParameter;
660
661                                 if (mb == null)
662                                         pb = cb.DefineParameter (
663                                                 i + 1, array_param.Attributes,
664                                                 array_param.Name);
665                                 else
666                                         pb = mb.DefineParameter (
667                                                 i + 1, array_param.Attributes,
668                                                 array_param.Name);
669                                         
670                                 CustomAttributeBuilder a = new CustomAttributeBuilder (
671                                         TypeManager.cons_param_array_attribute, new object [0]);
672                                 
673                                 pb.SetCustomAttribute (a);
674                         }
675                 }
676
677                 public Parameters Copy (Location l)
678                 {
679                         Parameters p = new Parameters (null, null, l);
680                         p.FixedParameters = new Parameter[this.FixedParameters.Length];
681                         this.FixedParameters.CopyTo (p.FixedParameters, 0);
682                 
683                         return (p);
684                 }
685
686                 public void AppendParameter (Parameter p)
687                 {
688                         if (FixedParameters != null) 
689                         {
690                                 Parameter [] pa = new Parameter [FixedParameters.Length+1];
691                                 FixedParameters.CopyTo (pa, 0);
692                                 pa[FixedParameters.Length] = p;
693                                 FixedParameters = pa;           
694                         }
695                         else
696                         {
697                                 FixedParameters = new Parameter [1];
698                                 FixedParameters[0] = p;
699                         }
700
701                 }
702         }
703 }