oops.
[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 (-28, location, "The Microsoft .NET Runtime 1.x does not permit setting custom attributes on the return type");
67                         }
68                 }
69
70                 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
71                 {
72                         if (a.Type == TypeManager.cls_compliant_attribute_type) {
73                                 Report.Warning (3023, 1, a.Location, "CLSCompliant attribute has no meaning when applied to return types. Try putting it on the method instead");
74                         }
75
76                         // This occurs after Warning -28
77                         if (builder == null)
78                                 return;
79
80                         base.ApplyAttributeBuilder (a, cb);
81                 }
82
83                 public override AttributeTargets AttributeTargets {
84                         get {
85                                 return AttributeTargets.ReturnValue;
86                         }
87                 }
88
89                 /// <summary>
90                 /// Is never called
91                 /// </summary>
92                 public override string[] ValidAttributeTargets {
93                         get {
94                                 return null;
95                         }
96                 }
97         }
98
99         /// <summary>
100         /// Class for applying custom attributes on the implicit parameter type
101         /// of the 'set' method in properties, and the 'add' and 'remove' methods in events.
102         /// </summary>
103         public class ImplicitParameter: ParameterBase {
104                 public ImplicitParameter (MethodBuilder mb):
105                         base (null)
106                 {
107                         builder = mb.DefineParameter (1, ParameterAttributes.None, "");                 
108                 }
109
110                 public override AttributeTargets AttributeTargets {
111                         get {
112                                 return AttributeTargets.Parameter;
113                         }
114                 }
115
116                 /// <summary>
117                 /// Is never called
118                 /// </summary>
119                 public override string[] ValidAttributeTargets {
120                         get {
121                                 return null;
122                         }
123                 }
124         }
125
126         /// <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 typeName;
276                         if (parameter_type != null)
277                                 typeName = TypeManager.CSharpName (parameter_type);
278                         else if (TypeName.Type != null)
279                                 typeName = TypeManager.CSharpName (TypeName.Type);
280                         else
281                                 typeName = TypeName.ToString ();
282
283                         switch (ModFlags & unchecked (~Modifier.ISBYREF)) {
284                                 case Modifier.OUT:
285                                         return "out " + typeName;
286                                 case Modifier.PARAMS:
287                                         return "params " + typeName;
288                                 case Modifier.REF:
289                                         return "ref " + typeName;
290                         }
291                         return typeName;
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 void ComputeSignature (EmitContext ec)
362                 {
363                         signature = "";
364                         if (FixedParameters != null){
365                                 for (int i = 0; i < FixedParameters.Length; i++){
366                                         Parameter par = FixedParameters [i];
367                                         
368                                         signature += par.GetSignature (ec, loc);
369                                 }
370                         }
371                         //
372                         // Note: as per the spec, the `params' arguments (ArrayParameter)
373                         // are not used in the signature computation for a method
374                         //
375                 }
376
377                 void Error_DuplicateParameterName (string name)
378                 {
379                         Report.Error (
380                                 100, loc, "The parameter name `" + name + "' is a duplicate");
381                 }
382                 
383                 public bool VerifyArgs ()
384                 {
385                         int count;
386                         int i, j;
387
388                         if (FixedParameters == null)
389                                 return true;
390                         
391                         count = FixedParameters.Length;
392                         string array_par_name = ArrayParameter != null ? ArrayParameter.Name : null;
393
394                         for (i = 0; i < count; i++){
395                                 string base_name = FixedParameters [i].Name;
396                                 for (j = i + 1; j < count; j++){
397                                         if (base_name != FixedParameters [j].Name)
398                                                 continue;
399                                         Error_DuplicateParameterName (base_name);
400                                         return false;
401                                 }
402
403                                 if (base_name == array_par_name){
404                                         Error_DuplicateParameterName (base_name);
405                                         return false;
406                                 }
407                         }
408                         return true;
409                 }
410                 
411                 /// <summary>
412                 ///    Returns the signature of the Parameters evaluated in
413                 ///    the @ec EmitContext
414                 /// </summary>
415                 public string GetSignature (EmitContext ec)
416                 {
417                         if (signature == null){
418                                 VerifyArgs ();
419                                 ComputeSignature (ec);
420                         }
421                         
422                         return signature;
423                 }
424                 
425                 /// <summary>
426                 ///    Returns the paramenter information based on the name
427                 /// </summary>
428                 public Parameter GetParameterByName (string name, out int idx)
429                 {
430                         idx = 0;
431                         int i = 0;
432
433                         if (FixedParameters != null){
434                                 foreach (Parameter par in FixedParameters){
435                                         if (par.Name == name){
436                                                 idx = i;
437                                                 return par;
438                                         }
439                                         i++;
440                                 }
441                         }
442
443                         if (ArrayParameter != null){
444                                 if (name == ArrayParameter.Name){
445                                         idx = i;
446                                         return ArrayParameter;
447                                 }
448                         }
449                         
450                         return null;
451                 }
452
453                 public Parameter GetParameterByName (string name)
454                 {
455                         int idx;
456
457                         return GetParameterByName (name, out idx);
458                 }
459                 
460                 bool ComputeParameterTypes (EmitContext ec)
461                 {
462                         int extra = (ArrayParameter != null) ? 1 : 0;
463                         int i = 0;
464                         int pc;
465
466                         if (FixedParameters == null)
467                                 pc = extra;
468                         else
469                                 pc = extra + FixedParameters.Length;
470
471                         types = new Type [pc];
472                         
473                         if (!VerifyArgs ()){
474                                 FixedParameters = null;
475                                 return false;
476                         }
477
478                         bool failed = false;
479                         if (FixedParameters != null){
480                                 foreach (Parameter p in FixedParameters){
481                                         Type t = null;
482                                         
483                                         if (p.Resolve (ec, loc))
484                                                 t = p.ExternalType ();
485                                         else
486                                                 failed = true;
487
488                                         types [i] = t;
489                                         i++;
490                                 }
491                         }
492                         
493                         if (extra > 0){
494                                 if (ArrayParameter.Resolve (ec, loc))
495                                         types [i] = ArrayParameter.ExternalType ();
496                                 else 
497                                         failed = true;
498                         }
499
500                         if (failed){
501                                 types = null;
502                                 return false;
503                         }
504
505                         return true;
506                 }
507
508                 //
509                 // This variant is used by Delegates, because they need to
510                 // resolve/define names, instead of the plain LookupType
511                 //
512                 public bool ComputeAndDefineParameterTypes (EmitContext ec)
513                 {
514                         bool old_type_resolving = ec.ResolvingTypeTree;
515                         ec.ResolvingTypeTree = true;
516                         bool retval = ComputeParameterTypes (ec);
517                         ec.ResolvingTypeTree = old_type_resolving;
518                         return retval;
519                 }
520                 
521                 /// <summary>
522                 ///   Returns the argument types as an array
523                 /// </summary>
524                 static Type [] no_types = new Type [0];
525                 
526                 public Type [] GetParameterInfo (EmitContext ec)
527                 {
528                         if (types != null)
529                                 return types;
530                         
531                         if (FixedParameters == null && ArrayParameter == null)
532                                 return no_types;
533
534                         if (ComputeParameterTypes (ec) == false){
535                                 types = null;
536                                 return null;
537                         }
538
539                         return types;
540                 }
541
542                 /// <summary>
543                 ///   Returns the type of a given parameter, and stores in the `is_out'
544                 ///   boolean whether this is an out or ref parameter.
545                 ///
546                 ///   Note that the returned type will not contain any dereference in this
547                 ///   case (ie, you get "int" for a ref int instead of "int&"
548                 /// </summary>
549                 public Type GetParameterInfo (EmitContext ec, int idx, out Parameter.Modifier mod)
550                 {
551                         mod = Parameter.Modifier.NONE;
552                         
553                         if (!VerifyArgs ()){
554                                 FixedParameters = null;
555                                 return null;
556                         }
557
558                         if (FixedParameters == null && ArrayParameter == null)
559                                 return null;
560                         
561                         if (types == null)
562                                 if (ComputeParameterTypes (ec) == false)
563                                         return null;
564
565                         //
566                         // If this is a request for the variable lenght arg.
567                         //
568                         int array_idx = (FixedParameters != null ? FixedParameters.Length : 0);
569                         if (idx == array_idx)
570                                 return types [idx];
571
572                         //
573                         // Otherwise, it is a fixed parameter
574                         //
575                         Parameter p = FixedParameters [idx];
576                         mod = p.ModFlags;
577
578                         if ((mod & (Parameter.Modifier.REF | Parameter.Modifier.OUT)) != 0)
579                                 mod |= Parameter.Modifier.ISBYREF;
580
581                         return p.ParameterType;
582                 }
583
584                 public CallingConventions GetCallingConvention ()
585                 {
586                         if (HasArglist)
587                                 return CallingConventions.VarArgs;
588                         else
589                                 return CallingConventions.Standard;
590                 }
591
592                 //
593                 // The method's attributes are passed in because we need to extract
594                 // the "return:" attribute from there to apply on the return type
595                 //
596                 public void LabelParameters (EmitContext ec,
597                         MethodBase builder,
598                         Location loc) {
599                         //
600                         // Define each type attribute (in/out/ref) and
601                         // the argument names.
602                         //
603                         int i = 0;
604                         
605                         MethodBuilder mb = builder as MethodBuilder;
606                         ConstructorBuilder cb = builder as ConstructorBuilder;
607
608                         if (FixedParameters != null) {
609                                 for (i = 0; i < FixedParameters.Length; i++) {
610                                         FixedParameters [i].DefineParameter (ec, mb, cb, i + 1, loc);
611                                 }
612                         }
613
614                         if (ArrayParameter != null){
615                                 ParameterBuilder pb;
616                                 Parameter array_param = ArrayParameter;
617
618                                 if (mb == null)
619                                         pb = cb.DefineParameter (
620                                                 i + 1, array_param.Attributes,
621                                                 array_param.Name);
622                                 else
623                                         pb = mb.DefineParameter (
624                                                 i + 1, array_param.Attributes,
625                                                 array_param.Name);
626                                         
627                                 CustomAttributeBuilder a = new CustomAttributeBuilder (
628                                         TypeManager.cons_param_array_attribute, new object [0]);
629                                 
630                                 pb.SetCustomAttribute (a);
631                         }
632                 }
633         }
634 }