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