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