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