2008-02-08 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / parameter.cs
1 //
2 // parameter.cs: Parameter definition.
3 //
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 //         Marek Safar (marek.safar@seznam.cz)
6 //
7 // Licensed under the terms of the GNU GPL
8 //
9 // (C) 2001 Ximian, Inc (http://www.ximian.com)
10 //
11 //
12 //
13 using System;
14 using System.Reflection;
15 using System.Reflection.Emit;
16 using System.Collections;
17 using System.Text;
18
19 namespace Mono.CSharp {
20
21         /// <summary>
22         ///   Abstract Base class for parameters of a method.
23         /// </summary>
24         public abstract class ParameterBase : Attributable {
25
26                 protected ParameterBuilder builder;
27
28                 protected ParameterBase (Attributes attrs)
29                         : base (attrs)
30                 {
31                 }
32
33                 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
34                 {
35                         if (a.Type == TypeManager.marshal_as_attr_type) {
36                                 UnmanagedMarshal marshal = a.GetMarshal (this);
37                                 if (marshal != null) {
38                                         builder.SetMarshal (marshal);
39                                 }
40                                 return;
41                         }
42
43                         if (a.Type.IsSubclassOf (TypeManager.security_attr_type)) {
44                                 a.Error_InvalidSecurityParent ();
45                                 return;
46                         }
47
48                         builder.SetCustomAttribute (cb);
49                 }
50
51                 public override bool IsClsComplianceRequired()
52                 {
53                         return false;
54                 }
55         }
56
57         /// <summary>
58         /// Class for applying custom attributes on the return type
59         /// </summary>
60         public class ReturnParameter : ParameterBase {
61                 public ReturnParameter (MethodBuilder mb, Location location):
62                         base (null)
63                 {
64                         try {
65                                 builder = mb.DefineParameter (0, ParameterAttributes.None, "");                 
66                         }
67                         catch (ArgumentOutOfRangeException) {
68                                 Report.RuntimeMissingSupport (location, "custom attributes on the return type");
69                         }
70                 }
71
72                 public override void ApplyAttributeBuilder(Attribute a, CustomAttributeBuilder cb)
73                 {
74                         if (a.Type == TypeManager.cls_compliant_attribute_type) {
75                                 Report.Warning (3023, 1, a.Location, "CLSCompliant attribute has no meaning when applied to return types. Try putting it on the method instead");
76                         }
77
78                         // This occurs after Warning -28
79                         if (builder == null)
80                                 return;
81
82                         base.ApplyAttributeBuilder (a, cb);
83                 }
84
85                 public override AttributeTargets AttributeTargets {
86                         get {
87                                 return AttributeTargets.ReturnValue;
88                         }
89                 }
90
91                 public override IResolveContext ResolveContext {
92                         get {
93                                 throw new NotSupportedException ();
94                         }
95                 }
96
97                 /// <summary>
98                 /// Is never called
99                 /// </summary>
100                 public override string[] ValidAttributeTargets {
101                         get {
102                                 return null;
103                         }
104                 }
105         }
106
107         /// <summary>
108         /// Class for applying custom attributes on the implicit parameter type
109         /// of the 'set' method in properties, and the 'add' and 'remove' methods in events.
110         /// </summary>
111         /// 
112         // TODO: should use more code from Parameter.ApplyAttributeBuilder
113         public class ImplicitParameter : ParameterBase {
114                 public ImplicitParameter (MethodBuilder mb):
115                         base (null)
116                 {
117                         builder = mb.DefineParameter (1, ParameterAttributes.None, "");                 
118                 }
119
120                 public override AttributeTargets AttributeTargets {
121                         get {
122                                 return AttributeTargets.Parameter;
123                         }
124                 }
125
126                 public override IResolveContext ResolveContext {
127                         get {
128                                 throw new NotSupportedException ();
129                         }
130                 }
131
132                 /// <summary>
133                 /// Is never called
134                 /// </summary>
135                 public override string[] ValidAttributeTargets {
136                         get {
137                                 return null;
138                         }
139                 }
140         }
141
142         public class ImplicitLambdaParameter : Parameter
143         {
144                 public ImplicitLambdaParameter (string name, Location loc)
145                         : base ((Type)null, name, Modifier.NONE, null, loc)
146                 {
147                 }
148
149                 public override bool Resolve (IResolveContext ec)
150                 {
151                         if (parameter_type == null)
152                                 throw new InternalErrorException ("A type of implicit lambda parameter `{0}' is not set",
153                                         Name);
154
155                         return true;
156                 }
157         }
158
159         public class ParamsParameter : Parameter {
160                 public ParamsParameter (Expression type, string name, Attributes attrs, Location loc):
161                         base (type, name, Parameter.Modifier.PARAMS, attrs, loc)
162                 {
163                 }
164
165                 public override bool Resolve (IResolveContext ec)
166                 {
167                         if (!base.Resolve (ec))
168                                 return false;
169
170                         if (!parameter_type.IsArray || parameter_type.GetArrayRank () != 1) {
171                                 Report.Error (225, Location, "The params parameter must be a single dimensional array");
172                                 return false;
173                         }
174                         return true;
175                 }
176
177                 public override void ApplyAttributes (MethodBuilder mb, ConstructorBuilder cb, int index)
178                 {
179                         base.ApplyAttributes (mb, cb, index);
180
181                         CustomAttributeBuilder a = new CustomAttributeBuilder (
182                                 TypeManager.cons_param_array_attribute, new object[0]);
183                                 
184                         builder.SetCustomAttribute (a);
185                 }
186         }
187
188         public class ArglistParameter : Parameter {
189                 // Doesn't have proper type because it's never chosen for better conversion
190                 public ArglistParameter () :
191                         base (typeof (ArglistParameter), String.Empty, Parameter.Modifier.ARGLIST, null, Location.Null)
192                 {
193                 }
194
195                 public override bool CheckAccessibility (InterfaceMemberBase member)
196                 {
197                         return true;
198                 }
199
200                 public override bool Resolve (IResolveContext ec)
201                 {
202                         return true;
203                 }
204
205                 public override string GetSignatureForError ()
206                 {
207                         return "__arglist";
208                 }
209         }
210
211         /// <summary>
212         ///   Represents a single method parameter
213         /// </summary>
214         public class Parameter : ParameterBase {
215                 [Flags]
216                 public enum Modifier : byte {
217                         NONE    = 0,
218                         REF     = REFMASK | ISBYREF,
219                         OUT     = OUTMASK | ISBYREF,
220                         PARAMS  = 4,
221                         // This is a flag which says that it's either REF or OUT.
222                         ISBYREF = 8,
223                         ARGLIST = 16,
224                         REFMASK = 32,
225                         OUTMASK = 64,
226                         This    = 128
227                 }
228
229                 static string[] attribute_targets = new string [] { "param" };
230
231                 Expression TypeName;
232                 public Modifier modFlags;
233                 public string Name;
234                 public bool IsCaptured;
235                 protected Type parameter_type;
236                 public readonly Location Location;
237
238                 IResolveContext resolve_context;
239                 LocalVariableReference expr_tree_variable;
240                 static TypeExpr parameter_expr_tree_type;
241
242                 Variable var;
243                 public Variable Variable {
244                         get { return var; }
245                 }
246
247 #if GMCS_SOURCE
248                 public bool IsTypeParameter;
249                 GenericConstraints constraints;
250 #else
251                 public bool IsTypeParameter {
252                         get {
253                                 return false;
254                         }
255                         set {
256                                 if (value)
257                                         throw new Exception ("You can not se TypeParameter in MCS");
258                         }
259                 }
260 #endif
261                 
262                 public Parameter (Expression type, string name, Modifier mod, Attributes attrs, Location loc)
263                         : this (type.Type, name, mod, attrs, loc)
264                 {
265                         if (type == TypeManager.system_void_expr)
266                                 Report.Error (1536, loc, "Invalid parameter type `void'");
267                         
268                         TypeName = type;
269                 }
270
271                 public Parameter (Type type, string name, Modifier mod, Attributes attrs, Location loc)
272                         : base (attrs)
273                 {
274                         Name = name;
275                         modFlags = mod;
276                         parameter_type = type;
277                         Location = loc;
278                 }
279
280                 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
281                 {
282                         if (a.Type == TypeManager.in_attribute_type && ModFlags == Modifier.OUT) {
283                                 Report.Error (36, a.Location, "An out parameter cannot have the `In' attribute");
284                                 return;
285                         }
286
287                         if (a.Type == TypeManager.param_array_type) {
288                                 Report.Error (674, a.Location, "Do not use `System.ParamArrayAttribute'. Use the `params' keyword instead");
289                                 return;
290                         }
291
292                         if (a.Type == TypeManager.out_attribute_type && (ModFlags & Modifier.REF) == Modifier.REF &&
293                             !OptAttributes.Contains (TypeManager.in_attribute_type)) {
294                                 Report.Error (662, a.Location,
295                                         "Cannot specify only `Out' attribute on a ref parameter. Use both `In' and `Out' attributes or neither");
296                                 return;
297                         }
298
299                         if (a.Type == TypeManager.cls_compliant_attribute_type) {
300                                 Report.Warning (3022, 1, a.Location, "CLSCompliant attribute has no meaning when applied to parameters. Try putting it on the method instead");
301                         }
302
303                         // TypeManager.default_parameter_value_attribute_type is null if !NET_2_0, or if System.dll is not referenced
304                         if (a.Type == TypeManager.default_parameter_value_attribute_type) {
305                                 object val = a.GetParameterDefaultValue ();
306                                 if (val != null) {
307                                         Type t = val.GetType ();
308                                         if (t.IsArray || TypeManager.IsSubclassOf (t, TypeManager.type_type)) {
309                                                 if (parameter_type == TypeManager.object_type) {
310                                                         if (!t.IsArray)
311                                                                 t = TypeManager.type_type;
312
313                                                         Report.Error (1910, a.Location, "Argument of type `{0}' is not applicable for the DefaultValue attribute",
314                                                                 TypeManager.CSharpName (t));
315                                                 } else {
316                                                         Report.Error (1909, a.Location, "The DefaultValue attribute is not applicable on parameters of type `{0}'",
317                                                                 TypeManager.CSharpName (parameter_type)); ;
318                                                 }
319                                                 return;
320                                         }
321                                 }
322
323                                 if (parameter_type == TypeManager.object_type ||
324                                     (val == null && !TypeManager.IsValueType (parameter_type)) ||
325                                     (val != null && TypeManager.TypeToCoreType (val.GetType ()) == parameter_type))
326                                         builder.SetConstant (val);
327                                 else
328                                         Report.Error (1908, a.Location, "The type of the default value should match the type of the parameter");
329                                 return;
330                         }
331
332                         base.ApplyAttributeBuilder (a, cb);
333                 }
334                 
335                 public virtual bool CheckAccessibility (InterfaceMemberBase member)
336                 {
337                         if (IsTypeParameter)
338                                 return true;
339
340                         return member.IsAccessibleAs (parameter_type);
341                 }
342
343                 public override IResolveContext ResolveContext {
344                         get {
345                                 return resolve_context;
346                         }
347                 }
348
349                 // <summary>
350                 //   Resolve is used in method definitions
351                 // </summary>
352                 public virtual bool Resolve (IResolveContext ec)
353                 {
354                         // HACK: to resolve attributes correctly
355                         this.resolve_context = ec;
356
357                         if (parameter_type != null)
358                                 return true;
359
360                         TypeExpr texpr = TypeName.ResolveAsTypeTerminal (ec, false);
361                         if (texpr == null)
362                                 return false;
363
364                         parameter_type = texpr.Type;
365
366 #if GMCS_SOURCE
367                         TypeParameterExpr tparam = texpr as TypeParameterExpr;
368                         if (tparam != null) {
369                                 IsTypeParameter = true;
370                                 constraints = tparam.TypeParameter.Constraints;
371                                 return true;
372                         }
373 #endif
374
375                         if ((parameter_type.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute) {
376                                 Report.Error (721, Location, "`{0}': static types cannot be used as parameters", 
377                                         texpr.GetSignatureForError ());
378                                 return false;
379                         }
380
381                         if ((modFlags & Parameter.Modifier.ISBYREF) != 0){
382                                 if (parameter_type == TypeManager.typed_reference_type ||
383                                     parameter_type == TypeManager.arg_iterator_type){
384                                         Report.Error (1601, Location, "Method or delegate parameter cannot be of type `{0}'",
385                                                 GetSignatureForError ());
386                                         return false;
387                                 }
388                         }
389
390                         if ((modFlags & Modifier.This) != 0 && parameter_type.IsPointer) {
391                                 Report.Error (1103, Location, "The type of extension method cannot be `{0}'",
392                                         TypeManager.CSharpName (parameter_type));
393                                 return false;
394                         }
395                         
396                         return true;
397                 }
398
399                 public void ResolveVariable (ToplevelBlock toplevel, int idx)
400                 {
401                         if (toplevel.RootScope != null)
402                                 var = toplevel.RootScope.GetCapturedParameter (this);
403                         if (var == null)
404                                 var = new ParameterVariable (this, idx);
405                 }
406
407                 public Type ExternalType ()
408                 {
409                         if ((modFlags & Parameter.Modifier.ISBYREF) != 0)
410                                 return TypeManager.GetReferenceType (parameter_type);
411                         
412                         return parameter_type;
413                 }
414
415                 public bool HasExtensionMethodModifier {
416                         get { return (modFlags & Modifier.This) != 0; }
417                 }
418
419                 public Modifier ModFlags {
420                         get { return modFlags & ~Modifier.This; }
421                 }
422
423                 public Type ParameterType {
424                         get {
425                                 return parameter_type;
426                         }
427                         set {
428                                 parameter_type = value;
429                                 IsTypeParameter = false;
430                         }
431                 }
432
433 #if GMCS_SOURCE
434                 public GenericConstraints GenericConstraints {
435                         get {
436                                 return constraints;
437                         }
438                 }
439 #endif
440
441                 ParameterAttributes Attributes {
442                         get {
443                                 return (modFlags & Modifier.OUT) == Modifier.OUT ?
444                                         ParameterAttributes.Out : ParameterAttributes.None;
445                         }
446                 }
447
448                 // TODO: should be removed !!!!!!!
449                 public static ParameterAttributes GetParameterAttributes (Modifier mod)
450                 {
451                         int flags = ((int) mod) & ~((int) Parameter.Modifier.ISBYREF);
452                         switch ((Modifier) flags) {
453                         case Modifier.NONE:
454                                 return ParameterAttributes.None;
455                         case Modifier.REF:
456                                 return ParameterAttributes.None;
457                         case Modifier.OUT:
458                                 return ParameterAttributes.Out;
459                         case Modifier.PARAMS:
460                                 return 0;
461                         }
462                                 
463                         return ParameterAttributes.None;
464                 }
465                 
466                 public override AttributeTargets AttributeTargets {
467                         get {
468                                 return AttributeTargets.Parameter;
469                         }
470                 }
471
472                 public virtual string GetSignatureForError ()
473                 {
474                         string type_name;
475                         if (parameter_type != null)
476                                 type_name = TypeManager.CSharpName (parameter_type);
477                         else
478                                 type_name = TypeName.GetSignatureForError ();
479
480                         string mod = GetModifierSignature (modFlags);
481                         if (mod.Length > 0)
482                                 return String.Concat (mod, ' ', type_name);
483
484                         return type_name;
485                 }
486
487                 public static string GetModifierSignature (Modifier mod)
488                 {
489                         switch (mod) {
490                                 case Modifier.OUT:
491                                         return "out";
492                                 case Modifier.PARAMS:
493                                         return "params";
494                                 case Modifier.REF:
495                                         return "ref";
496                                 case Modifier.ARGLIST:
497                                         return "__arglist";
498                                 case Modifier.This:
499                                         return "this";
500                                 default:
501                                         return "";
502                         }
503                 }
504
505                 public void IsClsCompliant ()
506                 {
507                         if (AttributeTester.IsClsCompliant (ExternalType ()))
508                                 return;
509
510                         Report.Error (3001, Location, "Argument type `{0}' is not CLS-compliant", GetSignatureForError ());
511                 }
512
513                 public virtual void ApplyAttributes (MethodBuilder mb, ConstructorBuilder cb, int index)
514                 {
515 #if !GMCS_SOURCE || !MS_COMPATIBLE
516                         // TODO: It should use mb.DefineGenericParameters
517                         if (mb == null)
518                                 builder = cb.DefineParameter (index, Attributes, Name);
519                         else 
520                                 builder = mb.DefineParameter (index, Attributes, Name);
521 #endif
522
523                         if (OptAttributes != null)
524                                 OptAttributes.Emit ();
525                 }
526
527                 public override string[] ValidAttributeTargets {
528                         get {
529                                 return attribute_targets;
530                         }
531                 }
532
533                 protected class ParameterVariable : Variable
534                 {
535                         public readonly Parameter Parameter;
536                         public readonly int Idx;
537                         public readonly bool IsRef;
538
539                         public ParameterVariable (Parameter par, int idx)
540                         {
541                                 this.Parameter = par;
542                                 this.Idx = idx;
543                                 this.IsRef = (par.ModFlags & Parameter.Modifier.ISBYREF) != 0;
544                         }
545
546                         public override Type Type {
547                                 get { return Parameter.ParameterType; }
548                         }
549
550                         public override bool HasInstance {
551                                 get { return false; }
552                         }
553
554                         public override bool NeedsTemporary {
555                                 get { return false; }
556                         }
557
558                         public override void EmitInstance (EmitContext ec)
559                         {
560                         }
561
562                         public override void Emit (EmitContext ec)
563                         {
564                                 int arg_idx = Idx;
565                                 if (!ec.MethodIsStatic)
566                                         arg_idx++;
567
568                                 ParameterReference.EmitLdArg (ec.ig, arg_idx);
569                         }
570
571                         public override void EmitAssign (EmitContext ec)
572                         {
573                                 int arg_idx = Idx;
574                                 if (!ec.MethodIsStatic)
575                                         arg_idx++;
576
577                                 if (arg_idx <= 255)
578                                         ec.ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
579                                 else
580                                         ec.ig.Emit (OpCodes.Starg, arg_idx);
581                         }
582
583                         public override void EmitAddressOf (EmitContext ec)
584                         {
585                                 int arg_idx = Idx;
586
587                                 if (!ec.MethodIsStatic)
588                                         arg_idx++;
589
590                                 if (IsRef) {
591                                         if (arg_idx <= 255)
592                                                 ec.ig.Emit (OpCodes.Ldarg_S, (byte) arg_idx);
593                                         else
594                                                 ec.ig.Emit (OpCodes.Ldarg, arg_idx);
595                                 } else {
596                                         if (arg_idx <= 255)
597                                                 ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
598                                         else
599                                                 ec.ig.Emit (OpCodes.Ldarga, arg_idx);
600                                 }
601                         }
602                 }
603
604                 public Parameter Clone ()
605                 {
606                         Parameter p = new Parameter (parameter_type, Name, ModFlags, attributes, Location);
607                         p.IsTypeParameter = IsTypeParameter;
608
609                         return p;
610                 }
611
612                 public ExpressionStatement CreateExpressionTreeVariable (EmitContext ec)
613                 {
614                         if ((modFlags & Modifier.ISBYREF) != 0)
615                                 Report.Error (1951, Location, "An expression tree parameter cannot use `ref' or `out' modifier");
616
617                         LocalInfo variable = ec.CurrentBlock.AddTemporaryVariable (
618                                 ResolveParameterExpressionType (ec, Location), Location);
619                         variable.Resolve (ec);
620
621                         expr_tree_variable = new LocalVariableReference (
622                                 ec.CurrentBlock, variable.Name, Location, variable, false);
623
624                         ArrayList arguments = new ArrayList (2);
625                         arguments.Add (new Argument (new TypeOf (
626                                 new TypeExpression (parameter_type, Location), Location)));
627                         arguments.Add (new Argument (new StringConstant (Name, Location)));
628                         return new Assign (ExpressionTreeVariableReference (),
629                                 Expression.CreateExpressionFactoryCall ("Parameter", null, arguments, Location));
630                 }
631
632                 public Expression ExpressionTreeVariableReference ()
633                 {
634                         return expr_tree_variable;
635                 }
636
637                 //
638                 // System.Linq.Expressions.ParameterExpression type
639                 //
640                 public static TypeExpr ResolveParameterExpressionType (EmitContext ec, Location location)
641                 {
642                         if (parameter_expr_tree_type != null)
643                                 return parameter_expr_tree_type;
644
645                         parameter_expr_tree_type = new TypeExpression (LinqExpression.parameter_expression_type, location).
646                                 ResolveAsTypeTerminal (ec, false);
647
648                         return parameter_expr_tree_type;
649                 }
650         }
651
652         /// <summary>
653         ///   Represents the methods parameters
654         /// </summary>
655         public class Parameters : ParameterData {
656                 // Null object pattern
657                 public Parameter [] FixedParameters;
658                 public readonly bool HasArglist;
659                 Type [] types;
660                 int count;
661
662                 public static readonly Parameters EmptyReadOnlyParameters = new Parameters ();
663                 static readonly Parameter ArgList = new ArglistParameter ();
664
665 #if GMCS_SOURCE
666 //              public readonly TypeParameter[] TypeParameters;
667 #endif
668
669                 private Parameters ()
670                 {
671                         FixedParameters = new Parameter[0];
672                         types = new Type [0];
673                 }
674
675                 private Parameters (Parameter[] parameters, Type[] types)
676                 {
677                         FixedParameters = parameters;
678                         this.types = types;
679                         count = types.Length;
680                 }
681                 
682                 public Parameters (params Parameter[] parameters)
683                 {
684                         if (parameters == null)
685                                 throw new ArgumentException ("Use EmptyReadOnlyPatameters");
686
687                         FixedParameters = parameters;
688                         count = parameters.Length;
689                 }
690
691                 public Parameters (Parameter[] parameters, bool has_arglist):
692                         this (parameters)
693                 {
694                         HasArglist = has_arglist;
695                 }
696                 
697                 public static Parameters CreateFullyResolved (Parameter p)
698                 {
699                         return new Parameters (new Parameter [] { p }, new Type [] { p.ParameterType });
700                 }
701                 
702                 public static Parameters CreateFullyResolved (Parameter[] parameters, Type[] types)
703                 {
704                         return new Parameters (parameters, types);
705                 }
706
707                 /// <summary>
708                 /// Use this method when you merge compiler generated argument with user arguments
709                 /// </summary>
710                 public static Parameters MergeGenerated (Parameters userParams, params Parameter[] compilerParams)
711                 {
712                         Parameter[] all_params = new Parameter [userParams.count + compilerParams.Length];
713                         Type[] all_types = new Type[all_params.Length];
714                         userParams.FixedParameters.CopyTo(all_params, 0);
715                         userParams.Types.CopyTo (all_types, 0);
716
717                         int last_filled = userParams.Count;
718                         foreach (Parameter p in compilerParams) {
719                                 for (int i = 0; i < last_filled; ++i) {
720                                         while (p.Name == all_params [i].Name) {
721                                                 p.Name = '_' + p.Name;
722                                         }
723                                 }
724                                 all_params [last_filled] = p;
725                                 all_types [last_filled] = p.ParameterType;
726                                 ++last_filled;
727                         }
728                         
729                         return new Parameters (all_params, all_types);
730                 }
731
732                 public bool Empty {
733                         get {
734                                 return count == 0;
735                         }
736                 }
737
738                 public int Count {
739                         get {
740                                 return HasArglist ? count + 1 : count;
741                         }
742                 }
743
744                 //
745                 // The property can be used after parameter types were resolved.
746                 //
747                 public Type ExtensionMethodType {
748                         get {
749                                 if (count == 0)
750                                         return null;
751
752                                 return FixedParameters [0].HasExtensionMethodModifier ?
753                                         types [0] : null;
754                         }
755                 }
756
757                 public bool HasExtensionMethodType {
758                         get {
759                                 if (count == 0)
760                                         return false;
761
762                                 return FixedParameters [0].HasExtensionMethodModifier;
763                         }
764                 }
765
766
767                 bool VerifyArgs ()
768                 {
769                         if (count < 2)
770                                 return true;
771
772                         for (int i = 0; i < count; i++){
773                                 string base_name = FixedParameters [i].Name;
774                                 for (int j = i + 1; j < count; j++){
775                                         if (base_name != FixedParameters [j].Name)
776                                                 continue;
777
778                                         Report.Error (100, FixedParameters [i].Location,
779                                                 "The parameter name `{0}' is a duplicate", base_name);
780                                         return false;
781                                 }
782                         }
783                         return true;
784                 }
785                 
786                 
787                 /// <summary>
788                 ///    Returns the paramenter information based on the name
789                 /// </summary>
790                 public Parameter GetParameterByName (string name, out int idx)
791                 {
792                         idx = 0;
793
794                         if (count == 0)
795                                 return null;
796
797                         int i = 0;
798
799                         foreach (Parameter par in FixedParameters){
800                                 if (par.Name == name){
801                                         idx = i;
802                                         return par;
803                                 }
804                                 i++;
805                         }
806                         return null;
807                 }
808
809                 public Parameter GetParameterByName (string name)
810                 {
811                         int idx;
812
813                         return GetParameterByName (name, out idx);
814                 }
815                 
816                 public bool Resolve (IResolveContext ec)
817                 {
818                         if (types != null)
819                                 return true;
820
821                         types = new Type [count];
822                         
823                         if (!VerifyArgs ())
824                                 return false;
825
826                         bool ok = true;
827                         Parameter p;
828                         for (int i = 0; i < FixedParameters.Length; ++i) {
829                                 p = FixedParameters [i];
830                                 if (!p.Resolve (ec)) {
831                                         ok = false;
832                                         continue;
833                                 }
834                                 types [i] = p.ExternalType ();
835                         }
836
837                         return ok;
838                 }
839
840                 public void ResolveVariable (ToplevelBlock toplevel)
841                 {
842                         for (int i = 0; i < FixedParameters.Length; ++i) {
843                                 Parameter p = FixedParameters [i];
844                                 p.ResolveVariable (toplevel, i);
845                         }
846                 }
847
848                 public CallingConventions CallingConvention
849                 {
850                         get {
851                                 if (HasArglist)
852                                         return CallingConventions.VarArgs;
853                                 else
854                                         return CallingConventions.Standard;
855                         }
856                 }
857
858                 // Define each type attribute (in/out/ref) and
859                 // the argument names.
860                 public void ApplyAttributes (MethodBase builder)
861                 {
862                         if (count == 0)
863                                 return;
864
865                         MethodBuilder mb = builder as MethodBuilder;
866                         ConstructorBuilder cb = builder as ConstructorBuilder;
867
868                         for (int i = 0; i < FixedParameters.Length; i++) {
869                                 FixedParameters [i].ApplyAttributes (mb, cb, i + 1);
870                         }
871                 }
872
873 #if MS_COMPATIBLE
874                 public ParameterData InflateTypes (Type[] genArguments, Type[] argTypes)
875                 {
876                         Parameters p = Clone ();
877                         for (int i = 0; i < count; ++i) {
878                                 if (types[i].IsGenericType) {
879                                         Type[] gen_arguments_open = new Type [types[i].GetGenericTypeDefinition ().GetGenericArguments ().Length];
880                                         Type[] gen_arguments = types[i].GetGenericArguments ();
881                                         for (int ii = 0; ii < gen_arguments_open.Length; ++ii) {
882                                                 if (gen_arguments[ii].IsGenericParameter) {
883                                                         Type t = argTypes[gen_arguments[ii].GenericParameterPosition];
884                                                         gen_arguments_open[ii] = t;
885                                                 } else
886                                                         gen_arguments_open[ii] = gen_arguments[ii];
887                                         }
888
889                                         p.FixedParameters [i].ParameterType = p.types[i] =
890                                                 types[i].GetGenericTypeDefinition ().MakeGenericType (gen_arguments_open);
891                                         continue;
892                                 }
893
894                                 if (types[i].IsGenericParameter) {
895                                         Type gen_argument = argTypes[types[i].GenericParameterPosition];
896                                         p.FixedParameters[i].ParameterType = p.types[i] = gen_argument;
897                                         continue;
898                                 }
899                         }
900
901                         return p;
902                 }
903 #endif
904
905                 public void VerifyClsCompliance ()
906                 {
907                         foreach (Parameter p in FixedParameters)
908                                 p.IsClsCompliant ();
909                 }
910
911                 public string GetSignatureForError ()
912                 {
913                         StringBuilder sb = new StringBuilder ("(");
914                         if (count > 0) {
915                                 for (int i = 0; i < FixedParameters.Length; ++i) {
916                                         sb.Append (FixedParameters[i].GetSignatureForError ());
917                                         if (i < FixedParameters.Length - 1)
918                                                 sb.Append (", ");
919                                 }
920                         }
921
922                         if (HasArglist) {
923                                 if (sb.Length > 1)
924                                         sb.Append (", ");
925                                 sb.Append ("__arglist");
926                         }
927
928                         sb.Append (')');
929                         return sb.ToString ();
930                 }
931
932                 public Type[] Types {
933                         get {
934                                 return types;
935                         }
936                         //
937                         // Dangerous, used by implicit lambda parameters
938                         // only to workaround bad design
939                         //
940                         set {
941                                 types = value;
942                         }
943                 }
944
945                 public Parameter this [int pos]
946                 {
947                         get {
948                                 if (pos >= count && (HasArglist || HasParams)) {
949                                         if (HasArglist && (pos == 0 || pos >= count))
950                                                 return ArgList;
951                                         pos = count - 1;
952                                 }
953
954                                 return FixedParameters [pos];
955                         }
956                 }
957
958                 #region ParameterData Members
959
960                 public Type ParameterType (int pos)
961                 {
962                         return this [pos].ExternalType ();
963                 }
964
965                 public bool HasParams {
966                         get {
967                                 if (count == 0)
968                                         return false;
969
970                                 for (int i = count; i != 0; --i) {
971                                         if ((FixedParameters [i - 1].ModFlags & Parameter.Modifier.PARAMS) != 0)
972                                                 return true;
973                                 }
974                                 return false;
975                         }
976                 }
977
978                 public string ParameterName (int pos)
979                 {
980                         return this [pos].Name;
981                 }
982
983                 public string ParameterDesc (int pos)
984                 {
985                         return this [pos].GetSignatureForError ();
986                 }
987
988                 public Parameter.Modifier ParameterModifier (int pos)
989                 {
990                         return this [pos].ModFlags;
991                 }
992
993                 public Expression CreateExpressionTree (EmitContext ec, Location loc)
994                 {
995                         ArrayList initializers = new ArrayList (count);
996                         foreach (Parameter p in FixedParameters) {
997                                 //
998                                 // Each parameter expression is stored to local variable
999                                 // to save some memory when referenced later.
1000                                 //
1001                                 StatementExpression se = new StatementExpression (p.CreateExpressionTreeVariable (ec));
1002                                 if (se.Resolve (ec))
1003                                         ec.CurrentBlock.AddScopeStatement (se);
1004                                 
1005                                 initializers.Add (p.ExpressionTreeVariableReference ());
1006                         }
1007
1008                         return new ArrayCreation (
1009                                 Parameter.ResolveParameterExpressionType (ec, loc),
1010                                 "[]", initializers, loc);
1011                 }
1012
1013                 public Parameters Clone ()
1014                 {
1015                         Parameter [] parameters_copy = new Parameter [FixedParameters.Length];
1016                         int i = 0;
1017                         foreach (Parameter p in FixedParameters)
1018                                 parameters_copy [i++] = p.Clone ();
1019                         Parameters ps = new Parameters (parameters_copy, HasArglist);
1020                         if (types != null)
1021                                 ps.types = (Type[])types.Clone ();
1022                         return ps;
1023                 }
1024                 
1025                 #endregion
1026         }
1027 }