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