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